开发者

Using a Regex in the URI of a Mongrel Handler

开发者 https://www.devze.com 2022-12-21 21:50 出处:网络
I\'m currently using Mongrel to develop a custom web application project. I would like Mongrel to use a defined Http Handler based on a regular expression. For example, everytime someone calls a url

I'm currently using Mongrel to develop a custom web application project.

I would like Mongrel to use a defined Http Handler based on a regular expression. For example, everytime someone calls a url like http://test/bla1.js or http://test/bla2.js the same Http handler is called to manage the request.

My code so far looks a like that:

http_server = Mongrel::Configurator.new :host => config.get("http_host") do
  listener :port => config.get("http_port") do

    uri Regexp.escape("/[a-z0-9]+.js"), :handler => BLAH::CustomHandler.new
    uri '/ui/public', :handler => Mongrel::DirHandler.new("#{$d}/public/")
    uri '/favicon', :handler => Mongrel::Error404Handler.new('')

    trap("INT") { stop }
    run
  end
end

As you can see, I am trying to use a regex instead of a s开发者_运维问答tring here:

 uri Regexp.escape("/[a-z0-9]+.js"), :handler => BLAH::CustomHandler.new

but that does not work. Any solution?

Thanks for that.


You should consider creating a Rack application instead. Rack is:

  • the standard for Ruby web applications
  • used internally by all popular Ruby web frameworks (Rails, Merb, Sinatra, Camping, Ramaze, ...)
  • much easier to extend
  • ready to be run on any application server (Mongrel, Webrick, Thin, Passenger, ...)

Rack has a URL mapping DSL, Rack::Builder, which allows you to map different Rack applications to particular URL prefixes. You typically save it as config.ru, and run it with rackup.

Unfortunately, it does not allow regular expressions either. But because of the simplicity of Rack, it is really easy to write an "application" (a lambda, actually) that will call the proper app if the URL matches a certain regex.

Based on your example, your config.ru may look something like this:

require "my_custom_rack_app" # Whatever provides your MyCustomRackApp.

js_handler = MyCustomRackApp.new

default_handlers = Rack::Builder.new do
  map "/public" do
    run Rack::Directory.new("my_dir/public")
  end

  # Uncomment this to replace Rack::Builder's 404 handler with your own:
  # map "/" do
  #   run lambda { |env|
  #     [404, {"Content-Type" => "text/plain"}, ["My 404 response"]]
  #   }
  # end
end

run lambda { |env|
  if env["PATH_INFO"] =~ %r{/[a-z0-9]+\.js}
    js_handler.call(env)
  else
    default_handlers.call(env)
  end
}

Next, run your Rack app on the command line:

% rackup

If you have mongrel installed, it will be started on port 9292. Done!


You have to inject new code into part of Mongrel's URIClassifier, which is otherwise blissfully unaware of regular expression URIs.

Below is one way of doing just that:

#
# Must do the following BEFORE Mongrel::Configurator.new
#  Augment some of the key methods in Mongrel::URIClassifier
#  See lib/ruby/gems/XXX/gems/mongrel-1.1.5/lib/mongrel/uri_classifier.rb
#
Mongrel::URIClassifier.class_eval <<-EOS, __FILE__, __LINE__
  # Save original methods
  alias_method :register_without_regexp, :register
  alias_method :unregister_without_regexp, :unregister
  alias_method :resolve_without_regexp, :resolve

  def register(uri, handler)
    if uri.is_a?(Regexp)
      unless (@regexp_handlers ||= []).any? { |(re,h)| re==uri ? h.concat(handler) : false }
        @regexp_handlers << [ uri, handler ]
      end
    else
      # Original behaviour
      register_without_regexp(uri, handler)
    end
  end

  def unregister(uri)
    if uri.is_a?(Regexp)
      raise Mongrel::URIClassifier::RegistrationError, "\#{uri.inspect} was not registered" unless (@regexp_handlers ||= []).reject! { |(re,h)| re==uri }
    else
      # Original behaviour
      unregister_without_regexp(uri)
    end
  end

  def resolve(request_uri)
    # Try original behaviour FIRST
    result = resolve_without_regexp(request_uri)
    # If a match is not found with non-regexp URIs, try regexp
    if result[0].blank?
      (@regexp_handlers ||= []).any? { |(re,h)| (m = re.match(request_uri)) ? (result = [ m.pre_match + m.to_s, (m.to_s == Mongrel::Const::SLASH ? request_uri : m.post_match), h ]) : false }
    end
    result
  end
EOS

http_server = Mongrel::Configurator.new :host => config.get("http_host") do 
  listener :port => config.get("http_port") do 

    # Can pass a regular expression as URI
    #  (URI must be of type Regexp, no escaping please!)
    # Regular expression can match any part of an URL, start with "^/..." to
    #  anchor match at URI beginning.
    # The way this is implemented, regexp matches are only evaluated AFTER
    #  all non-regexp matches have failed (mostly for performance reasons.)
    # Also, for regexp URIs, the :in_front is ignored; adding multiple handlers
    #  to the same URI regexp behaves as if :in_front => false
    uri /^[a-z0-9]+.js/, :handler => BLAH::CustomHandler.new 

    uri '/ui/public', :handler => Mongrel::DirHandler.new("#{$d}/public/") 
    uri '/favicon', :handler => Mongrel::Error404Handler.new('') 

    trap("INT") { stop } 
    run 
  end 
end

Seems to work just fine with Mongrel 1.1.5.

0

精彩评论

暂无评论...
验证码 换一张
取 消