Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent pipe character from causing a Bad URI Error in Rails 3/Ruby 1.9.2?

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.

like image 645
ryonlife Avatar asked Sep 20 '10 05:09

ryonlife


3 Answers

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|"

like image 169
tardate Avatar answered Nov 15 '22 10:11

tardate


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
like image 7
dallasrpi Avatar answered Nov 15 '22 09:11

dallasrpi


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

like image 1
ryonlife Avatar answered Nov 15 '22 11:11

ryonlife