| Class | Mongrel::URIClassifier |
| In: |
lib/mongrel/uri_classifier.rb
lib/mongrel/uri_classifier.rb |
| Parent: | Object |
| handler_map | [R] | |
| handler_map | [R] |
# File lib/mongrel/uri_classifier.rb, line 17
17: def initialize
18: @handler_map = {}
19: @matcher = //
20: @root_handler = nil
21: end
# File lib/mongrel/uri_classifier.rb, line 17
17: def initialize
18: @handler_map = {}
19: @matcher = //
20: @root_handler = nil
21: end
Register a handler object at a particular URI. The handler can be whatever you want, including an array. It‘s up to you what to do with it.
Registering a handler is not necessarily threadsafe, so be careful if you go mucking around once the server is running.
# File lib/mongrel/uri_classifier.rb, line 28
28: def register(uri, handler)
29: raise RegistrationError, "#{uri.inspect} is already registered" if @handler_map[uri]
30: raise RegistrationError, "URI is empty" if !uri or uri.empty?
31: raise RegistrationError, "URI must begin with a \"#{Const::SLASH}\"" unless uri[0..0] == Const::SLASH
32: @handler_map[uri.dup] = handler
33: rebuild
34: end
Register a handler object at a particular URI. The handler can be whatever you want, including an array. It‘s up to you what to do with it.
Registering a handler is not necessarily threadsafe, so be careful if you go mucking around once the server is running.
# File lib/mongrel/uri_classifier.rb, line 28
28: def register(uri, handler)
29: raise RegistrationError, "#{uri.inspect} is already registered" if @handler_map[uri]
30: raise RegistrationError, "URI is empty" if !uri or uri.empty?
31: raise RegistrationError, "URI must begin with a \"#{Const::SLASH}\"" unless uri[0..0] == Const::SLASH
32: @handler_map[uri.dup] = handler
33: rebuild
34: end
Resolve a request URI by finding the best partial match in the registered handler URIs.
# File lib/mongrel/uri_classifier.rb, line 46
46: def resolve(request_uri)
47: if @root_handler
48: # Optimization for the pathological case of only one handler on "/"; e.g. Rails
49: [Const::SLASH, request_uri, @root_handler]
50: elsif match = @matcher.match(request_uri)
51: uri = match.to_s
52: # A root mounted ("/") handler must resolve such that path info matches the original URI.
53: [uri, (uri == Const::SLASH ? request_uri : match.post_match), @handler_map[uri]]
54: else
55: [nil, nil, nil]
56: end
57: end
Resolve a request URI by finding the best partial match in the registered handler URIs.
# File lib/mongrel/uri_classifier.rb, line 46
46: def resolve(request_uri)
47: if @root_handler
48: # Optimization for the pathological case of only one handler on "/"; e.g. Rails
49: [Const::SLASH, request_uri, @root_handler]
50: elsif match = @matcher.match(request_uri)
51: uri = match.to_s
52: # A root mounted ("/") handler must resolve such that path info matches the original URI.
53: [uri, (uri == Const::SLASH ? request_uri : match.post_match), @handler_map[uri]]
54: else
55: [nil, nil, nil]
56: end
57: end
Unregister a particular URI and its handler.
# File lib/mongrel/uri_classifier.rb, line 37
37: def unregister(uri)
38: handler = @handler_map.delete(uri)
39: raise RegistrationError, "#{uri.inspect} was not registered" unless handler
40: rebuild
41: handler
42: end
Unregister a particular URI and its handler.
# File lib/mongrel/uri_classifier.rb, line 37
37: def unregister(uri)
38: handler = @handler_map.delete(uri)
39: raise RegistrationError, "#{uri.inspect} was not registered" unless handler
40: rebuild
41: handler
42: end