Class: Anorexic::HTTPRouter

Inherits:
Object
  • Object
show all
Defined in:
lib/anorexic/handlers/http_router.rb

Overview

this is a Handler stub class for an HTTP echo server.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHTTPRouter

initializes an HTTP router (the normal Handler for HTTP requests)

the router holds the different hosts and sends them messages/requests.


14
15
16
17
# File 'lib/anorexic/handlers/http_router.rb', line 14

def initialize
	@hosts = {}
	@active_host = nil
end

Instance Attribute Details

#active_hostObject (readonly)

the current active host object


9
10
11
# File 'lib/anorexic/handlers/http_router.rb', line 9

def active_host
  @active_host
end

#hostsObject (readonly)

the hosts dictionary router.hosts == HTTPHost


7
8
9
# File 'lib/anorexic/handlers/http_router.rb', line 7

def hosts
  @hosts
end

Instance Method Details

#add_alias(host_name, *aliases) ⇒ Object

adds an alias to an existing host name (normally through the :alias parameter in the `add_host` method).


27
28
29
30
31
# File 'lib/anorexic/handlers/http_router.rb', line 27

def add_alias host_name, *aliases
	return false unless @hosts[host_name]
	aliases.each {|a| @hosts[a.to_s.downcase] = @hosts[host_name]}
	true
end

#add_host(host_name, params) ⇒ Object

adds a host to the router (or activates an existing host to add new routes). accepts a host name and any parameters not related to the service (see `Anorexic.add_service`)


20
21
22
23
24
25
# File 'lib/anorexic/handlers/http_router.rb', line 20

def add_host host_name, params
	host_name = (host_name ? host_name.to_s.downcase : :default)
	@hosts[host_name] ||= HTTPHost.new params
	add_alias host_name, *params[:alias] if params[:alias]
	@active_host = @hosts[host_name]
end

#add_route(path, controller, &block) ⇒ Object

adds a route to the active host. The active host is the last host referenced by the `add_host`.


34
35
36
37
# File 'lib/anorexic/handlers/http_router.rb', line 34

def add_route path, controller, &block
	raise 'No Host defined.' unless @active_host
	@active_host.add_route path, controller, &block
end

#add_shared_route(path, controller, &block) ⇒ Object

adds a route to all existing hosts.


40
41
42
43
# File 'lib/anorexic/handlers/http_router.rb', line 40

def add_shared_route path, controller, &block
	raise 'No Host defined.' if @hosts.empty?
	@hosts.each {|n, h| h.add_route path, controller, &block }
end

#call(env) ⇒ Object

handles requests send by Rack - dresses up as Middleware, for Rack (if you're don't like WebSockets, go ahead…)


58
59
60
61
62
63
64
65
66
# File 'lib/anorexic/handlers/http_router.rb', line 58

def call env
	if env["HOST"] && hosts[env["HOST"].downcase]
		hosts[env["HOST"].downcase].call env
	elsif hosts[:default]
		hosts[:default].call env
	else
		[404, {"content-type" => "text/plain", "content-length" => "15"}, ["host not found."] ]
	end
end

#on_request(request) ⇒ Object

handles requests send by the HTTP Protocol (HTTPRequest objects)


46
47
48
49
50
51
52
53
54
55
56
# File 'lib/anorexic/handlers/http_router.rb', line 46

def on_request request
	request.service.timeout = 300
	if request[:host_name] && hosts[request[:host_name].to_s.downcase]
		hosts[request[:host_name].downcase].on_request request
	elsif hosts[:default]
		hosts[:default].on_request request
	else
		HTTPResponse.new( request, 404, {"content-type" => "text/plain", "content-length" => "15"}, ["host not found."]).finish
	end
	request.service.timeout = 5
end