如何防止管道字符在 Rails 3/Ruby 1.9.2 中导致 Bad URI 错误?
在我的应用程序中实现 OAuth2 时,我需要处理如下 URI:
http://localhost: 3000/sessions/create/?code=lorem|ipsum
不确定这是 Rails 3 还是 Ruby 1.9.2 问题(可能是 URI.parse),但无论如何,WEBrick 会踢 Error bad URI< /代码>。
有人知道解决方法吗?谢谢。
In implementing OAuth2 in my app, I need to handle URIs like:
http://localhost:3000/sessions/create/?code=lorem|ipsum
Not sure if it's a Rails 3 or Ruby 1.9.2 problem (maybe URI.parse), but in any event, WEBrick kicks Error bad URI
.
Anyone know of a workaround? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我最近遇到了同样的要求(和问题)。在 Rails 3 和 Ruby 1.9.2 上。
对于我们的临时/生产环境(nginx)来说这不是问题,但我有兴趣找出 WEBrick 的问题所在。事实证明,问题出在 URI::Parser.split 方法中,特别是它的模式匹配如何使用 URI::REGEXP::PATTERN 常量进行播种。
您可以通过将以下内容添加到 config/environments/development.rb 来“修复”此问题(假设您只在开发中使用 WEBrick ..或者您可以将其放入 config/initializers 文件中)..
注意:这就是设置: 未保留 => “-_.!~*'()a-zA-Z\d|”
I ran into the same requirement (and problem) recently. On Rails 3 and Ruby 1.9.2.
It is not a problem for our staging/production environment (nginx), but I was interested to find out what the problem was with WEBrick. Turns out the issue is down in the URI::Parser.split method, specifically how it's pattern matching is seeded with the URI::REGEXP::PATTERN constants.
You can "fix" this by adding the following to a config/environments/development.rb (assuming you'd only be using WEBrick in dev .. or you could put it in a config/initializers file)..
NB: that's setting :UNRESERVED => "-_.!~*'()a-zA-Z\d|"
初始化器有效,但我最终使用 URI.escape 代替,因为它看起来更干净并且看起来可以处理更多情况。
另外这段代码似乎不正确
The initializer worked, but I ended up using URI.escape instead as it seemed cleaner and looks like it will handle more cases.
Plus this code just didnt seem right
我最终只是将 Thin 换成了 WEBrick,并没有遇到任何问题。
I ended up just swapping in Thin for WEBrick and haven't had issues.