alexleutgoeb alexleutgoeb - 1 month ago 16
Ruby Question

Sinatra redirect to URI without path

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

itms-services://?action=download-manifest&url=https://builds.net/abc.plist


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=https://builds.net/abc.plist'

# Raw headers redirect
status 302
headers 'Location' => 'itms-services://?action=download-manifest&url=https://builds.net/abc.plist'

# Halt redirect
halt 302, { 'Location' => 'itms-services://?action=download-manifest&url=https://builds.net/abc.plist' }, ''


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

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::Parser.new(:HOSTNAME => "(?:)|(?:#{::URI::REGEXP::PATTERN::HOSTNAME})").parse(uri)
  end
end
Comments