2012-01-13 107 views
2

使用ruby 1.9.2-p290。我碰到一个问题来试图解析URI像下面这样:解析具有大括号的URI,URI :: InvalidURIError:错误的URI(不是URI?)

require 'uri' 
my_uri = "http://www.anyserver.com/getdata?anyparameter={330C-B5A2}" 
the_uri = URI.parse(my_uri) 

发出以下错误:

URI::InvalidURIError: bad URI(is not URI?) 

我需要比每次都是这样编码的花括号中的不同的解决方案:

new_uri = URI.encode("http://www.anyserver.com/getdata?anyparameter={330C-B5A2}") 
=> "http://www.anyserver.com/getdata?anyparameter=%7B330C-B5A2%7D" 

现在我可以像往常一样解析new_uri,但每次需要时都必须执行此操作。不用每次都做到这一点,最简单的方法是什么?

我发布我自己的解决方案,因为我没有看到这完全像我解决它。


# Accepts URIs when they contain curly braces 
# This overrides the DEFAULT_PARSER with the UNRESERVED key, including '{' and '}' 
module URI 
    def self.parse(uri) 
    URI::Parser.new(:UNRESERVED => URI::REGEXP::PATTERN::UNRESERVED + "\{\}").parse(uri) 
    end 
end 

现在我可以用含有花括号URI使用URI.parse(URI),并不会引发错误。

+0

为什么你必须用URI解析它?你是否正在使用URI对URL进行其他操作,还是有其他参数需要编码? – 2012-01-13 07:45:16

+0

是的,基本上我修改了一个广泛使用它的gem,并且替换所有代码并不是很好,所以我更喜欢在一个地方更改URI#parse行为:) – 2012-01-13 14:20:24

回答

5
# Need to not fail when uri contains curly braces 
# This overrides the DEFAULT_PARSER with the UNRESERVED key, including '{' and '}' 
# DEFAULT_PARSER is used everywhere, so its better to override it once 
module URI 
    remove_const :DEFAULT_PARSER 
    unreserved = REGEXP::PATTERN::UNRESERVED 
    DEFAULT_PARSER = Parser.new(:UNRESERVED => unreserved + "\{\}") 
end 

跟进同样的问题,因为DEFAULT_PARSER到处使用,它能够更好地替代它完全insted的只是为URI#解析方法。另外,这样可以避免每次为实例化新的Parser对象分配内存。

+0

你应该接受你的答案。它的工作原理,谢谢! – mydoghasworms 2012-11-13 05:40:52

2

RFC 1738 - http://www.faqs.org/rfcs/rfc1738.html意味着你必须编码括号

Thus, only alphanumerics, the special characters "$-_.+!*'(),", and 
reserved characters used for their reserved purposes may be used 
unencoded within a URL. 
+0

谢谢!,但是我没有太多一个选择,因为uri是由一个外部服务提供的,并没有完全遵循RFC,所以使用了花括号:) – 2012-01-13 04:01:59