Solved

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

Posted on 2004-08-25
4
366 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

785 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