Class: Doze::Responder::Main

Inherits:
Doze::Responder show all
Defined in:
lib/doze/responder/main.rb

Constant Summary

Constants included from Utils

Utils::URI_SCHEMES

Instance Attribute Summary

Attributes inherited from Doze::Responder

#request

Instance Method Summary collapse

Methods inherited from Doze::Responder

#auth_failed_response, #call, #error_response, #error_response_from_error, #initialize, #raise_error, #recognized_method

Methods included from Utils

#escape, #quote, #request_base_uri, #unescape

Constructor Details

This class inherits a constructor from Doze::Responder

Instance Method Details

#responseObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/doze/responder/main.rb', line 3

def response
  resource = nil
  route_to = @app.root
  remaining_path = @request.routing_path
  remaining_path = nil if remaining_path.empty? || remaining_path == '/'
  session = @request.session
  base_uri = ''

  # main routing loop - results in either a final Resource which has been routed to, or nil
  # todo: maybe something recursive would be a bit more readable here
  while true
    if remaining_path && route_to.is_a?(Doze::Router)
      # Bail early with a 401 or 403 if the router refuses to authorize further routing
      return auth_failed_response unless route_to.authorize_routing(@request.session)
      route_to, base_uri, remaining_path = route_to.perform_routing(remaining_path, session, base_uri)
    elsif !remaining_path && route_to.is_a?(Doze::Resource)
      resource = route_to
      break
    else
      break
    end
  end

  if resource
    Doze::Responder::Resource.new(@app, @request, resource).response
  elsif @request.options?
    if @request.raw_path_info == '*'
      # Special response for "OPTIONS *" as in HTTP spec:
      rec_methods = (@app.config[:recognized_methods] + [:head, :options]).join(', ').upcase
      Doze::Response.new(STATUS_NO_CONTENT, 'Allow' => rec_methods)
    else
      # Special OPTIONS response for non-existent resource:
      Doze::Response.new(STATUS_NO_CONTENT, 'Allow' => 'OPTIONS')
    end
  else
    error_response(STATUS_NOT_FOUND)
  end
end