如何防止管道字符导致 Rails 3/Ruby 1.9.2 中的错误 URI 错误? [英] How to prevent pipe character from causing a Bad URI Error in Rails 3/Ruby 1.9.2?

查看:33
本文介绍了如何防止管道字符导致 Rails 3/Ruby 1.9.2 中的错误 URI 错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用中实现 OAuth2 时,我需要处理如下 URI:

In implementing OAuth2 in my app, I need to handle URIs like:

http://localhost:3000/sessions/create/?code=lorem|ipsum

不确定是 Rails 3 还是 Ruby 1.9.2 问题(可能是 URI.parse),但无论如何,WEBrick 会踢Error bad URI.

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.

推荐答案

我最近遇到了同样的需求(和问题).在 Rails 3 和 Ruby 1.9.2 上.

I ran into the same requirement (and problem) recently. On Rails 3 and Ruby 1.9.2.

这对我们的登台/生产环境 (nginx) 来说不是问题,但我很想知道 WEBrick 的问题是什么.原来问题出在 URI::Parser.split 方法中,特别是它的模式匹配是如何用 URI::REGEXP::PATTERN 常量播种的.

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.

您可以通过将以下内容添加到 config/environments/development.rb 来修复"此问题(假设您只在 dev 中使用 WEBrick .. 或者您可以将其放在 config/initializers 文件中)..

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)..

# this allows WEBrick to handle pipe symbols in query parameters
URI::DEFAULT_PARSER = 
  URI::Parser.new(:UNRESERVED => URI::REGEXP::PATTERN::UNRESERVED + '|')

注意:这是设置 :UNRESERVED => "-_.!~*'()a-zA-Z\d|"

NB: that's setting :UNRESERVED => "-_.!~*'()a-zA-Z\d|"

这篇关于如何防止管道字符导致 Rails 3/Ruby 1.9.2 中的错误 URI 错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆