alexleutgoeb alexleutgoeb - 1 year ago 206
Ruby Question

Sinatra redirect to URI without path

I'm trying a simple redirect to an OTA installation link for iOS of the pattern:


Regardless of the tried redirect method Sinatra (webrick?) however always complains about an invalid URI:
ERROR URI::InvalidURIError: bad URI(absolute but no path):

I already tried the following calls:

# Simple redirect
redirect 'itms-services://?action=download-manifest&url='

# Raw headers redirect
status 302
headers 'Location' => 'itms-services://?action=download-manifest&url='

# Halt redirect
halt 302, { 'Location' => 'itms-services://?action=download-manifest&url=' }, ''

always resulting in the same error.

Is there any possibility to do a redirect without triggering the parsing of URI class?

(Sinatra 1.4.3, Ruby 1.9.3p194)

Answer Source

I finally came up with a work-around which also works on Webrick, this snippet however disables URI path checks at all:

# Add empty pattern for URI hostname parsing to allow itms-services://? URIs
module URI
  def self.parse(uri) => "(?:)|(?:#{::URI::REGEXP::PATTERN::HOSTNAME})").parse(uri)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download