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

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


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 parametersURI::DEFAULT_PARSER =   URI::Parser.new(:UNRESERVED => URI::REGEXP::PATTERN::UNRESERVED + '|')

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


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 '|' symbolsold_verbose = $VERBOSE$VERBOSE = nilURI::DEFAULT_PARSER = URI::Parser.new(:UNRESERVED => URI::REGEXP::PATTERN::UNRESERVED + '|')$VERBOSE = old_verbose


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