URI::InvalidURIError(URI 只能是 ascii) [英] URI::InvalidURIError (URI must be ascii only)

查看:53
本文介绍了URI::InvalidURIError(URI 只能是 ascii)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个旧版 rails 3.2 应用程序,当我尝试使用非 ascii 字符访问路由时,例如;example.com/city/bergstraße 然后我收到以下错误:

/Users/user/.rvm/rubies/ruby-2.2.5/lib/ruby/2.2.0/uri/rfc3986_parser.rb:20:in `split'/Users/user/.rvm/rubies/ruby-2.2.5/lib/ruby/2.2.0/uri/rfc3986_parser.rb:72:in `parse'/Users/user/.rvm/rubies/ruby-2.2.5/lib/ruby/2.2.0/uri/common.rb:226:in `parse'actionpack (3.1.0) lib/action_dispatch/routing/redirection.rb:91:in `block in redirection_proc'机架安装 (0.8.3) lib/rack/mount/route_set.rb:152:in `call'机架安装 (0.8.3) lib/rack/mount/route_set.rb:152:in `block in call'...

问题是,我在点击任何控制器之前收到此错误,所以我想知道在哪里可以捕获此错误以解析和修复 URL?谢谢.

P.S:在将我指向任何以前的帖子之前,请先查看堆栈跟踪.

解决方案

根据 RFC 3986 一个 URI 可能只包含 ASCII 字符的一个子集.

要提供有效的 URI,应转义非 ASCII 字符:

irb(main):008:0>URI.parse "example.com/city/#{URI.encode('bergstraße')}"=>#<URI::Generic example.com/city/bergstra%C3%9Fe>

<块引用>

问题是,我在点击任何控制器之前收到此错误,所以我只是想知道我在哪里可以捕捉到这个错误来解析和修复网址?

问题是你不应该真正捕捉到这个错误.您的 rails 服务器不应负责响应错误或格式错误的请求.

虽然您可以尝试编写一个中间件来解决这个问题,但您应该弄清楚为什么客户端会发送对无效 URI 的请求.

如果它们源自您自己的应用程序,请确保您正确地转义了 slug 列,而不仅仅是创建带有字符串插值的 url.stringex gem 或 friendly_id 是您的朋友.

I have a legacy rails 3.2 app, when I try to hit a route with a none-ascii char e.g; example.com/city/bergstraße then I get the following error:

/Users/user/.rvm/rubies/ruby-2.2.5/lib/ruby/2.2.0/uri/rfc3986_parser.rb:20:in `split'
/Users/user/.rvm/rubies/ruby-2.2.5/lib/ruby/2.2.0/uri/rfc3986_parser.rb:72:in `parse'
/Users/user/.rvm/rubies/ruby-2.2.5/lib/ruby/2.2.0/uri/common.rb:226:in `parse'
actionpack (3.1.0) lib/action_dispatch/routing/redirection.rb:91:in `block in redirection_proc'
rack-mount (0.8.3) lib/rack/mount/route_set.rb:152:in `call'
rack-mount (0.8.3) lib/rack/mount/route_set.rb:152:in `block in call'
...

The problem is, that I get this error before hitting any controller, so I just wonder where can I catch this error to parse and fix the URL? thanks.

P.S: please have a look to the stacktrace before pointing me to any previous post.

解决方案

According to RFC 3986 a URI may contain only a subset of ASCII characters.

To provide a valid URI the non-ASCII characters should be escaped:

irb(main):008:0> URI.parse "example.com/city/#{URI.encode('bergstraße')}"
=> #<URI::Generic example.com/city/bergstra%C3%9Fe>

The problem is, that I get this error before hitting any controller, so I just wonder where can I catch this error to parse and fix the URL?

The problem is you should not really be catching this error. Your rails server should not be responsible for responding to bad or malformed requests.

While you could attempt to write a piece of middleware to hack around the issue you should instead figure out why the clients are sending requests for an invalid URI.

If they are originating from your own application make sure you are escaping slug columns properly and not just creating urls with string interpolation. The stringex gem or friendly_id are your friends here.

这篇关于URI::InvalidURIError(URI 只能是 ascii)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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