且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

解析带有花括号的URI,URI :: InvalidURIError:错误的URI(不是URI吗?)

更新时间:2023-11-18 22:56:34

# 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随处可见,因此***完全替换为URI#parse方法.此外,这避免了每次为新的Parser对象的实例化分配内存.

Following up the same issue, since DEFAULT_PARSER is used everywhere, its better to substitute it completely insted of just for the URI#parse method. Additionally this avoids allocating memory for the instantiation of a new Parser object every time.