Re: URI Reference creation

I felt like starting some pseudocode for reference creation
my own. The code below aims to convert an absolute URI to
relative given a base URI if possible (meaning if scheme
and host are equal to those in the base URI).
Or was there pseudocode already and I forgot about it?

Here is what I currently have:



== Variables ==
Base - [IN] Base to cut off if possible
A    - [IN] Absolute URI that we would like relative instead
T    - [OUT] Transformation result (relative if possible, absolute if necessary)


== Example ==
BASE = http://ex/x/y/z?one
A    = http://ex/r?two
T    = ../../r?two


== Algorithm ==
if (A.scheme != Base.scheme) then
   T.scheme    = A.scheme;
   T.authority = A.authority;
   T.path      = A.path;
   T.query     = A.query;
   T.fragment  = A.fragment;
else
   undef(T.scheme);
   if (A.authority != Base.authority) then
      T.authority = A.authority;
      T.path      = A.path;
      T.query     = A.query;
      T.fragment  = A.fragment;
   else
      undef(last(Base.path));
      T.path = "";
      while (first(A.path) == first(Base.path)) do
         A.path++;
         Base.path++;
      endwhile;
      bool pathNaked = true;
      while defined(first(Base.path)) do
         Base.path++;
         T.path += "../";
         pathNaked = false;
      endwhile;
      while defined(first(A.path)) do
         if pathNaked then
            if (first(A.path) contains ":") then
               T.path += "./";
            endif;
         endif;
         T.path += first(A.path);
         pathNaked = false;
         A.path++;
         if defined(first(A.path)) then
            T.path += + "/";
         endif;
      endwhile;
      T.query    = A.query;
      T.fragment = A.fragment;
   endif;
endif;


I hope it's more useful than braindead, I'm not too awake anymore ;-)



Sebastian

Received on Wednesday, 5 September 2007 02:22:11 UTC