Solved

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

Posted on 2004-08-25
4
347 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
Comment Utility
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
Comment Utility
That would be fine.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

762 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

11 Experts available now in Live!

Get 1:1 Help Now