How to prevent pipe character from causing a Bad U

2019-02-06 08:00发布

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.

3条回答
ら.Afraid
2楼-- · 2019-02-06 08:29

The initializer worked, but I ended up using URI.escape instead as it seemed cleaner and looks like it will handle more cases.

URI.join(origin_url, URI.escape(parsed_link)).to_s

Plus this code just didnt seem right

# I need this because URI.join in crawler.rb bombs with '|' symbols
old_verbose = $VERBOSE
$VERBOSE = nil
URI::DEFAULT_PARSER = URI::Parser.new(:UNRESERVED => URI::REGEXP::PATTERN::UNRESERVED + '|')
$VERBOSE = old_verbose
查看更多
家丑人穷心不美
3楼-- · 2019-02-06 08:38

I ended up just swapping in Thin for WEBrick and haven't had issues.

查看更多
甜甜的少女心
4楼-- · 2019-02-06 08:40

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

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

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

查看更多
登录 后发表回答