Solved

Combine a base URL and a relative (partial) URL into an absolute (canonical) URL

Posted on 2004-08-25
4
359 Views
Last Modified: 2010-04-05
I desire a function that will combine a base URL and a relative (partial) URL into an absolute (canonical) URL.

Example inputs and outputs:

base = 'http://a/b/c'
relative = 'd'
result = 'http://a/b/c/d'

base = 'http://a/b/c'
relative = '../d'
result = 'http://a/b/d'

base = 'http://a/b/c'
relative = '../../d'
result = 'http://a/d'

base = 'http://a/b/c'
relative = '/d'
result = 'http://d'

The presence or absense of a trailing delimiter ('/') in the base URL should make no difference in the result.

This should be a relatively simple function, but I'm feeling lazy, and would like something that's been tested thoroughly.
0
Comment
Question by:gary_williams
  • 2
4 Comments
 
LVL 2

Accepted Solution

by:
gary_williams earned 0 total points
ID: 11899465
Well, I finally spent ten minutes and wrote one myself anyway.  I would have preferred a more robust version, but this will suffice.  Here's the code, in case anyone is interested.

function RelativeToAbsoluteURL(const BaseURL: String;
                               const RelativeURL: String): String;
var
  BaseParts: TStrings;
  RelativeParts: TStrings;
  I: Integer;
begin
  Assert(Copy(BaseURL, 1, 7) = 'http://');

  if (Length(RelativeURL) = 0) then
    Result := BaseURL
  else
    begin
    BaseParts := TStringList.Create;
    RelativeParts := TStringList.Create;
    try
      BaseParts.Delimiter := '/';
      BaseParts.DelimitedText := Copy(BaseURL, 8, Length(BaseURL) - 7);

      // remove blanks caused by a double slash or a slash at either end
      for I := (BaseParts.Count - 1) downto 0 do
        if (BaseParts[I] = '') then
          BaseParts.Delete(I);

      RelativeParts.Delimiter := '/';
      RelativeParts.DelimitedText := RelativeURL;

      // remove blanks caused by a double slash or a slash at either end
      for I := (RelativeParts.Count - 1) downto 0 do
        if (RelativeParts[I] = '') then
          RelativeParts.Delete(I);

      if (RelativeURL[1] = '/') then
        BaseParts.Clear
      else
        while (RelativeParts.Count > 0) and
              (RelativeParts[0] = '..') do
          begin
          RelativeParts.Delete(0);
          BaseParts.Delete(BaseParts.Count - 1);
          end;

      BaseParts.AddStrings(RelativeParts);
      Result := 'http://' + BaseParts.DelimitedText
    finally
      RelativeParts.Free;
      BaseParts.Free;
    end;
    end;
end;


...

var
  Test: String;

initialization

  Test := RelativeToAbsoluteURL('http://a/b/c', 'd');
  Assert(Test = 'http://a/b/c/d');

  Test := RelativeToAbsoluteURL('http://a/b/c', '../d');
  Assert(Test = 'http://a/b/d');

  Test := RelativeToAbsoluteURL('http://a/b/c', '../../d');
  Assert(Test = 'http://a/d');

  Test := RelativeToAbsoluteURL('http://a/b/c', '/d');
  Assert(Test = 'http://d');

  Test := RelativeToAbsoluteURL('http://a/b/c/', 'd');
  Assert(Test = 'http://a/b/c/d');

  Test := RelativeToAbsoluteURL('http://a/b/c/', '../d');
  Assert(Test = 'http://a/b/d');

  Test := RelativeToAbsoluteURL('http://a/b/c/', '../../d');
  Assert(Test = 'http://a/d');

  Test := RelativeToAbsoluteURL('http://a/b/c/', '/d');
  Assert(Test = 'http://d');

0
 
LVL 2

Author Comment

by:gary_williams
ID: 11901923
That would be fine.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now