Module: OpenID::URINorm

Defined in:
lib/openid/urinorm.rb

Class Method Summary collapse

Class Method Details

.urinorm(uri) ⇒ Object

Raises:

  • (URI::InvalidURIError)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/openid/urinorm.rb', line 9

def URINorm.urinorm(uri)
  uri = URI.parse(uri)

  raise URI::InvalidURIError.new('no scheme') unless uri.scheme
  uri.scheme = uri.scheme.downcase
  unless ['http','https'].member?(uri.scheme)
    raise URI::InvalidURIError.new('Not an HTTP or HTTPS URI')
  end

  raise URI::InvalidURIError.new('no host') unless uri.host
  uri.host = uri.host.downcase

  uri.path = remove_dot_segments(uri.path)
  uri.path = '/' if uri.path.length == 0

  uri = uri.normalize.to_s
  uri = uri.gsub(PERCENT_ESCAPE_RE) {
    sub = $&[1..2].to_i(16).chr
    reserved(sub) ? $&.upcase : sub
  }

  return uri
end