Skip to content

Commit

Permalink
removed dead code, add no_response method
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Hull committed Apr 26, 2011
1 parent 3dde8fa commit 360dfc5
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 18 deletions.
24 changes: 13 additions & 11 deletions lib/http_router.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

class HttpRouter

attr_reader :root, :routes, :known_methods, :named_routes, :request_methods
attr_reader :root, :routes, :known_methods, :named_routes
attr_accessor :default_app, :url_mount

# Raised when a Route is not able to be generated.
Expand All @@ -29,15 +29,13 @@ class HttpRouter
# * :ignore_trailing_slash -- Ignore a trailing / when attempting to match. Defaults to +true+.
# * :redirect_trailing_slash -- On trailing /, redirect to the same path without the /. Defaults to +false+.
# * :known_methods -- Array of http methods tested for 405s.
# * :request_methods -- Array of methods to use on request
def initialize(*args, &blk)
default_app, options = args.first.is_a?(Hash) ? [nil, args.first] : [args.first, args[1]]
@options = options
@default_app = default_app || options && options[:default_app] || proc{|env| ::Rack::Response.new("Not Found", 404, {'X-Cascade' => 'pass'}).finish }
@ignore_trailing_slash = options && options.key?(:ignore_trailing_slash) ? options[:ignore_trailing_slash] : true
@redirect_trailing_slash = options && options.key?(:redirect_trailing_slash) ? options[:redirect_trailing_slash] : false
@known_methods = Set.new(options && options[:known_methods] || [])
@request_methods = options && options[:request_methods] || [:host, :scheme, :request_method, :user_agent]
reset!
instance_eval(&blk) if blk
end
Expand Down Expand Up @@ -123,14 +121,7 @@ def call(env, perform_call = true)
request = Request.new(rack_request.path_info, rack_request, perform_call)
response = catch(:success) { @root[request] }
if response.nil?
supported_methods = (@known_methods - [env['REQUEST_METHOD']]).select do |m|
test_env = ::Rack::Request.new(rack_request.env.clone)
test_env.env['REQUEST_METHOD'] = m
test_env.env['_HTTP_ROUTER_405_TESTING_ACCEPTANCE'] = true
test_request = Request.new(test_env.path_info, test_env, 405)
catch(:success) { @root[test_request] }
end
supported_methods.empty? ? (perform_call ? @default_app.call(env) : nil) : [405, {'Allow' => supported_methods.sort.join(", ")}, []]
no_response(env, perform_call)
elsif response
response
elsif perform_call
Expand Down Expand Up @@ -203,6 +194,17 @@ def clone(klass = self.class)
end

private
def no_response(env, perform_call = true)
supported_methods = (@known_methods - [env['REQUEST_METHOD']]).select do |m|
test_env = ::Rack::Request.new(env.clone)
test_env.env['REQUEST_METHOD'] = m
test_env.env['_HTTP_ROUTER_405_TESTING_ACCEPTANCE'] = true
test_request = Request.new(test_env.path_info, test_env, 405)
catch(:success) { @root[test_request] }
end
supported_methods.empty? ? (perform_call ? @default_app.call(env) : nil) : [405, {'Allow' => supported_methods.sort.join(", ")}, []]
end

def add_with_request_method(path, method, opts = {}, &app)
route = add(path, opts).send(method.to_sym)
route.to(app) if app
Expand Down
7 changes: 2 additions & 5 deletions lib/http_router/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,8 @@ def add(matcher)
end

def unescape(val)
val.to_s.gsub(/((?:%[0-9a-fA-F]{2})+)/n){ [$1.delete('%')].pack('H*') }
end

def join_whole_path(request)
request.path * '/'
val.to_s.gsub!(/((?:%[0-9a-fA-F]{2})+)/n){ [$1.delete('%')].pack('H*') }
val
end
end
end
2 changes: 1 addition & 1 deletion lib/http_router/node/free_regex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def initialize(router, matcher)
end

def [](request)
whole_path = "/#{join_whole_path(request)}"
whole_path = "/#{request.joined_path}"
if match = @matcher.match(whole_path) and match[0].size == whole_path.size
request = request.clone
request.extra_env['router.regex_match'] = match
Expand Down
2 changes: 1 addition & 1 deletion lib/http_router/node/spanning_regex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class HttpRouter
class Node
class SpanningRegex < Regex
def [](request)
whole_path = join_whole_path(request)
whole_path = request.joined_path
if match = @matcher.match(whole_path) and match.begin(0).zero?
request = request.clone
add_params(request, match)
Expand Down
4 changes: 4 additions & 0 deletions lib/http_router/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ def initialize(path, rack_request, perform_call)
@params = []
end

def joined_path
@path * '/'
end

def perform_call
@perform_call == true
end
Expand Down

0 comments on commit 360dfc5

Please sign in to comment.