在 Mongrel 处理程序的 URI 中使用正则表达式
我目前正在使用 Mongrel 开发自定义 Web 应用程序项目。
我希望 Mongrel 使用基于正则表达式定义的 Http 处理程序。例如,每次有人调用 http://test/bla1.js 或 http://test/bla2.js 调用相同的 Http 处理程序来管理请求。
到目前为止,我的代码看起来像这样:
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
如您所见,我尝试在此处使用正则表达式而不是字符串:
uri Regexp.escape("/[a-z0-9]+.js"), :handler => BLAH::CustomHandler.new
但这不起作用。有什么解决办法吗?
谢谢你。
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 string here:
uri Regexp.escape("/[a-z0-9]+.js"), :handler => BLAH::CustomHandler.new
but that does not work. Any solution?
Thanks for that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该考虑创建一个 Rack 应用程序。 Rack 是:
Rack 有一个 URL 映射 DSL,Rack::Builder,它允许您将不同的 Rack 应用程序映射到特定的 URL 前缀。您通常将其保存为
config.ru
,并使用rackup
运行它。不幸的是,它也不允许正则表达式。但由于 Rack 的简单性,编写一个“应用程序”(实际上是一个 lambda )非常容易,如果 URL 与某个正则表达式匹配,它将调用正确的应用程序。
根据您的示例,您的 config.ru 可能如下所示:
接下来,在命令行上运行您的 Rack 应用程序:
如果您安装了 mongrel,它将在端口 9292 上启动。 完成!
You should consider creating a Rack application instead. Rack is:
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 withrackup
.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:Next, run your Rack app on the command line:
If you have mongrel installed, it will be started on port 9292. Done!
您必须将新代码注入 Mongrel 的
URIClassifier
的一部分,否则它根本不知道正则表达式 URI。下面是实现这一点的一种方法:
似乎与 Mongrel 1.1.5 配合得很好。
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:
Seems to work just fine with Mongrel 1.1.5.