Module: Alt::URI::Algorithm

Defined in:
lib/rio/alturi/algorithm.rb

Class Method Summary collapse

Class Method Details

.authority_string(r) ⇒ Object



90
91
92
93
94
# File 'lib/rio/alturi/algorithm.rb', line 90

def self.authority_string(r)
  ( defined(r.userinfo) ? r.userinfo + "@"  : "") + 
    r.host +
    ( defined(r.port) ? ":" + r.port : "") 
end

.defined(el) ⇒ Object



99
100
101
# File 'lib/rio/alturi/algorithm.rb', line 99

def self.defined(el)
  !el.nil?
end

.dump_bufs(inp, out) ⇒ Object



278
279
280
# File 'lib/rio/alturi/algorithm.rb', line 278

def self.dump_bufs(inp,out)
  #printf("inp:[%-15s]  out:[%-15s]\n",inp,out)
end

.merge(base, r) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
# File 'lib/rio/alturi/algorithm.rb', line 170

def self.merge(base,r)
  if defined(base.authority) and base.path.empty?
    "/"
  else
    if npos = base.path.rindex("/")
      base.path[0,npos+1]
    else
      ""
    end
  end + r.path
end

.rel_path(path, base) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rio/alturi/algorithm.rb', line 22

def self.rel_path(path,base)
  bs = base.split("/",-1)
  pa = path.split("/",-1)
  bsize = bs.size
  psize = pa.size
  out = []
  (0...[bsize,psize].min).each do |i|
    out[0] = pa[i]
    if bs[i] != pa[i]
      i += 1
      out.unshift(*([".."] * (bsize - i)))
      out += pa[i,psize-1]
      return out.join("/")
    end
  end
  if bsize > psize
    out.unshift(*([".."] * (bsize - psize)))
  elsif psize > bsize
    out += pa[bsize,psize-bsize]
  end
  return out.join("/")
end

.relative(t, base) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/rio/alturi/algorithm.rb', line 5

def self.relative(t,base)
  r = t.class.new
  if t.scheme != base.scheme
    r = t
  else
    if t.authority != base.authority
      r = t
    else
      r.path = rel_path(t.path,base.path)
      r.path = "." if r.path.empty?
      r.query = t.query
      r.fragment = t.fragment
    end
  end
  return r 
end

.remove_dot_segments(instr) ⇒ Object

  1. Finally, the output buffer is returned as the result of

    remove_dot_segments.
    


224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/rio/alturi/algorithm.rb', line 224

def self.remove_dot_segments(instr)
  #    1.  The input buffer is initialized with the now-appended path
  #        components and the output buffer is initialized to the empty
  #        string.
  inp = instr.dup
  out = ""

  #    2.  While the input buffer is not empty, loop as follows:
  while !inp.empty?

    #        A.  If the input buffer begins with a prefix of "../" or "./",
    #            then remove that prefix from the input buffer; otherwise,
    unless inp.sub!( %r'^\.\.?/' , '')

      #        B.  if the input buffer begins with a prefix of "/./" or "/.",
      #            where "." is a complete path segment, then replace that
      #            prefix with "/" in the input buffer; otherwise,
      unless inp.sub!( %r'^/\.(/|\Z)' ,"/" )#

        #        C.  if the input buffer begins with a prefix of "/../" or "/..",
        #            where ".." is a complete path segment, then replace that
        #            prefix with "/" in the input buffer and remove the last
        #            segment and its preceding "/" (if any) from the output
        #            buffer; otherwise,
        if inp.sub!( %r'^/\.\.(/|\Z)' ,"/" )
          out.sub!(%r'/?[^/]*\Z',"")
        else

          #        D.  if the input buffer consists only of "." or "..", then remove
          #            that from the input buffer; otherwise,
          unless inp.sub!( /^\.\.?$/, "")

            #        E.  move the first path segment in the input buffer to the end of
            #            the output buffer, including the initial "/" character (if
            #            any) and any subsequent characters up to, but not including,
            #            the next "/" character or the end of the input buffer.
            inp.sub!( %r'^(/?[^/]*)' ) { |match|
              out << match
              ""
            }
            
          end
        end
      end
    end
  end

  #    3.  Finally, the output buffer is returned as the result of
  #        remove_dot_segments.
  out

end

.remove_dot_segments0(inp) ⇒ Object



282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/rio/alturi/algorithm.rb', line 282

def self.remove_dot_segments0(inp)
  out = ""
  while !inp.empty?
    unless inp.sub!( %r'^\.\.?/' , "")
      unless inp.sub!( %r'^/\.(/|\Z)' ,"/" )
        if inp.sub!( %r'^/\.\.(/|\Z)' ,"/" )
          out.sub!(%r'/?[^/]*\Z',"")
        else
          unless inp.sub!( /^\.\.?$/, "")
            inp.sub!( %r'^(/?[^/]*)' ) { |match|
              out << match
              ""
            }
            
          end
        end
      end
    end
  end
  out
end

.transform(r, base, strict = true) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/rio/alturi/algorithm.rb', line 103

def self.transform(r,base,strict=true)

  #-- A non-strict parser may ignore a scheme in the reference
  #-- if it is identical to the base URI's scheme.
  #--
  if ((not strict) and (r.scheme == base.scheme))
    r.scheme = nil
  end

  t = r.class.new
  if defined(r.scheme) 
    t.scheme    = r.scheme;
    t.authority = r.authority;
    t.path      = remove_dot_segments(r.path);
    t.query     = r.query;
  else
    if defined(r.authority) 
      t.authority = r.authority;
      t.path      = remove_dot_segments(r.path);
      t.query     = r.query;
    else
      if (r.path == "") 
        t.path = base.path;
        
        if defined(r.query)
          t.query = r.query;
        else
          t.query = base.query;
        end
      else
        if (r.path.start_with?("/")) then
          t.path = remove_dot_segments(r.path);
        else
          t.path = merge(base, r);
          t.path = remove_dot_segments(t.path);
        end
        t.query = r.query;
      end
      t.authority = base.authority;
    end
    t.scheme = base.scheme;
  end
  t.fragment = r.fragment;

  return t
end

.uri_string(r) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/rio/alturi/algorithm.rb', line 83

def self.uri_string(r)
  ( defined(r.scheme) ? r.scheme + ":"  : "") + 
    ( defined(r.authority) ? "//" + r.authority : "") + 
    r.path +
    ( defined(r.query) ? "?" + r.query : "") +
    ( defined(r.fragment) ? "#" + r.fragment : "") 
end