Class: Hobix::WebApp::URIGen

Inherits:
Object
  • Object
show all
Defined in:
lib/hobix/webapp/urigen.rb

Overview

:stopdoc:

Constant Summary collapse

Alpha =
'a-zA-Z'
Digit =
'0-9'
AlphaNum =
Alpha + Digit
Mark =
'\-_.!~*\'()'
Unreserved =
AlphaNum + Mark
PChar =
Unreserved + ':@&=+$,'
Reserved =
';/?:@&=+$,'
Uric =
Reserved + Unreserved

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scheme, server_name, server_port, script_name, path_info) ⇒ URIGen

Returns a new instance of URIGen.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/hobix/webapp/urigen.rb', line 7

def initialize(scheme, server_name, server_port, script_name, path_info)
  @scheme = scheme
  @server_name = server_name
  @server_port = server_port
  @script_name = script_name
  @path_info = path_info
  uri = "#{scheme}://#{server_name}:#{server_port}"
  uri << script_name.gsub(%r{[^/]+}) {|segment| pchar_escape(segment) }
  uri << path_info.gsub(%r{[^/]+}) {|segment| pchar_escape(segment) }
  @base_uri = URI.parse(uri)
end

Instance Attribute Details

#base_uriObject (readonly)

Returns the value of attribute base_uri.



18
19
20
# File 'lib/hobix/webapp/urigen.rb', line 18

def base_uri
  @base_uri
end

Instance Method Details

#form_escape(s) ⇒ Object



133
134
135
136
137
# File 'lib/hobix/webapp/urigen.rb', line 133

def form_escape(s)
  s.gsub(/[#{Reserved}\x00-\x1f\x7f-\xff]/on) {|c|
    sprintf("%%%02X", c[0])
  }.gsub(/ /on) { '+' }
end

#make_absolute_uri(hash) ⇒ Object



113
114
115
# File 'lib/hobix/webapp/urigen.rb', line 113

def make_absolute_uri(hash)
  @base_uri + make_relative_uri(hash)
end

#make_relative_uri(hash) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/hobix/webapp/urigen.rb', line 20

def make_relative_uri(hash)
  script = nil
  path_info = nil
  query = nil
  fragment = nil
  hash.each_pair {|k,v|
    case k
    when :script then script = v
    when :path_info then path_info = v
    when :query then query = v
    when :fragment then fragment = v
    else
      raise ArgumentError, "unexpected: #{k} => #{v}"
    end
  }

  if !script
    script = @script_name
  elsif %r{\A/} !~ script
    script = @script_name.sub(%r{[^/]*\z}) { script }
    while script.sub!(%r{/[^/]*/\.\.(?=/|\z)}, '')
    end
    script.sub!(%r{\A/\.\.(?=/|\z)}, '')
  end

  path_info = '/' + path_info if %r{\A[^/]} =~ path_info

  dst = "#{script}#{path_info}"
  dst.sub!(%r{\A/}, '')
  dst.sub!(%r{[^/]*\z}, '')
  dst_basename = $&

  src = "#{@script_name}#{@path_info}"
  src.sub!(%r{\A/}, '')
  src.sub!(%r{[^/]*\z}, '')

  while src[%r{\A[^/]*/}] == dst[%r{\A[^/]*/}]
    if $~
      src.sub!(%r{\A[^/]*/}, '')
      dst.sub!(%r{\A[^/]*/}, '')
    else
      break
    end
  end

  rel_path = '../' * src.count('/')
  rel_path << dst << dst_basename
  rel_path = './' if rel_path.empty?

  rel_path.gsub!(%r{[^/]+}) {|segment| pchar_escape(segment) }
  if /\A[A-Za-z][A-Za-z0-9+\-.]*:/ =~ rel_path # It seems absolute URI.
    rel_path.sub!(/:/, '%3A')
  end

  if query
    case query
    when QueryString
      query = query.instance_eval { @escaped_query_string }
    when Hash
      query = query.map {|k, v|
        case v
        when String
          "#{form_escape(k)}=#{form_escape(v)}"
        when Array
          v.map {|e|
            unless String === e
              raise ArgumentError, "unexpected query value: #{e.inspect}"
            end
            "#{form_escape(k)}=#{form_escape(e)}"
          }
        else
          raise ArgumentError, "unexpected query value: #{v.inspect}"
        end
      }.join(';')
    else
      raise ArgumentError, "unexpected query: #{query.inspect}"
    end
    unless query.empty?
      query = '?' + uric_escape(query)
    end
  else
    query = ''
  end

  if fragment
    fragment = "#" + uric_escape(fragment)
  else
    fragment = ''
  end

  URI.parse(rel_path + query + fragment)
end

#pchar_escape(s) ⇒ Object



123
124
125
# File 'lib/hobix/webapp/urigen.rb', line 123

def pchar_escape(s)
  s.gsub(/[^#{PChar}]/on) {|c| sprintf("%%%02X", c[0]) }
end

#uric_escape(s) ⇒ Object



129
130
131
# File 'lib/hobix/webapp/urigen.rb', line 129

def uric_escape(s)
  s.gsub(/[^#{Uric}]/on) {|c| sprintf("%%%02X", c[0]) }
end