home account info subscribe login search FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Advanced Visual Basic Techniques
(Publisher: John Wiley & Sons, Inc.)
Author(s): Rod Stephens
ISBN: 0471188816
Publication Date: 06/01/97

Search this book:
 
Previous Table of Contents Next


Understanding URLs

As far as SiteMapper is concerned, a URL has four pieces:

protocol://site/path/file

The pieces are described in the following list:

•  protocol. The protocol tells Web browsers what methods to use when manipulating the file. This can be http, ftp, gopher, nntp, and so on.
•  site. This is the name of the Web site that holds the document. In general, a URL may contain other information here including a user name and password or a port number. For example, in “rod:let_me_in@www.mysite.com:80” the user name is “rod,” the password is “let_me_in,” and the Web site uses port number 80. SiteMapper treats all of these components as part of the site name.
•  path. This is the directory path to the file on the Web site. A complete URL always contains a path, even if it is just “/” indicating the document is in the site’s root directory.
•  file. This is the name of the document. The name may include a marker within the document. For example, the address “File1.HTM#marker” indicates the Web browser should jump to the location “marker” within the file File1.HTM. The marker is defined in the document with the following HTML code:
<A NAME=“marker”>Text displayed at this point</A>

SiteMapper ignores markers because it does not matter what part of a document the program visits. When the program opens a document, it reads the entire file including all of the marked locations.

Many documents contain partial URLs. Missing pieces of the URL are filled in using the components of the current document’s URL. For instance, suppose the document located at http://www.mysite.com/Public/Index.HTM contains the following reference:

<A HREF=“Details.HTM”>Jump to details page.</A>

The URL Details.HTM does not contain a protocol, site, or path. They are filled in using the values given by the current document. The complete URL is http://www.mysite.com/Public/Details.HTM.

Table 19.1 shows some partial URLs and their corresponding complete URLs. The table assumes the URLs are being read from the file http://site/dir1/dir2/File1.HTM. SiteMapper must be able to parse each of these different kinds of URLs correctly.

ParseURL

SiteMapper uses the ParseURL subroutine to separate URLs into their pieces. ParseURL begins by removing markers from the URL. It does this by stripping out any text that comes after a number sign. If the original URL was http://www.mysite.com/dir1/dir2/File.HTM#marker, the address becomes http://www.mysite.com/dir1/dir2/File.HTM.

Table 19.1 Partial and Complete URLs
Location of file Partial URL Complete URL
Same directory File2.HTM http://site/dir1/dir2/File2.HTM
Subdirectory subdir/File2.HTM http://site/dir1/dir2/subdir/ File2.HTM
Root directory /File2.HTM http://site/ File2.HTM
Another directory /path2/File2.HTM http://site/path2/ File2.HTM
Parent directory ../File2.HTM http://site/dir1/ File2.HTM
Subdirectory of the parent directory ../subdir/File2.HTM http://site/dir1/subdir/ File2.HTM

Next, ParseURL locates the address’s protocol. The routine assumes the protocol is followed by the string “://.” After recording the protocol, ParseURL removes it from the URL string. The example URL becomes www.mysite.com/dir1/dir2/File.HTM.

If the protocol is not present, the URL is a partial URL that should not include a site either. Otherwise, the site name includes the text up to the next slash. The routine saves the site name and removes it from the URL, giving /dir1/dir2/File.HTM.

ParseURL then locates the file name in the URL by finding the rightmost slash. Whatever text comes after this slash must be the file name. If this text includes a period as in File.HTM, ParseURL records the file name and removes it from the URL. Otherwise, the subroutine assumes this text is a subdirectory name and there is no file name in the URL. For example, the URL /dir1/dir2 contains no period after the rightmost slash so ParseURL assumes dir2 is a directory name.

Whatever remains of the text is the path. If ParseURL found a site name, then the path must be absolutely not relative. In this case, ParseURL makes sure the path begins with a slash. The subroutine also makes sure the path includes a trailing slash whether this is an absolute path or not.

Sub ParseURL(ByVal the_url As String, the_protocol As String, _
    the_site As String, the_path As String, the_file As String)
Dim pos As Integer
Dim got_site As Boolean

    the_url = Trim$(the_url)

    ‘ Remove any reference to a marker.
    pos = InStr(the_url, “#”)
    If pos > 0 Then
        the_url = Left$(the_url, pos - 1)
    End If

    ‘ Find the protocol.
    pos = InStr(the_url, “://”)
    If pos = 0 Then
        ‘ There is no protocol or site.
        the_protocol = NO_PROTOCOL
        the_site = NO_SITE
        got_site = False
    Else
        ‘ Record the protocol.
        the_protocol = Left$(the_url, pos - 1)

        ‘ Remove the protocol from the URL.
        the_url = Right$(the_url, Len(the_url) - pos - 2)

        ‘ Find the site.
        If the_url = “” Then
            ‘ There is no site.
            the_site = NO_SITE
            got_site = False
        Else
            pos = InStr(the_url, “/”)
            If pos = 0 Then pos = Len(the_url) + 1
            the_site = Left$(the_url, pos - 1)
            got_site = True

            ‘ Remove the site from the URL.
            the_url = Right$(the_url, Len(the_url) - pos + 1)

            ‘ If there’s nothing left, use the
            ‘ root path.
            If the_url = “” Then the_url = “/”
        End If
    End If

    ‘ Find the file.
    If the_url = “” Then
        ‘ There is no file.
        the_file = NO_FILE
    Else
        ‘ Find the last “/”.
        pos = 1
        Do While InStr(pos, the_url, “/”) > 0
            pos = InStr(pos, the_url, “/”) + 1
        Loop
        If pos > Len(the_url) Then
            ‘ The url ends with a “/”. No file.
            the_file = NO_FILE
        Else
            ‘ See if the “file” has a “.” in it.
            If InStr(pos, the_url, “.”) > 0 Then
                ‘ It does. This is the file.
                the_file = Right$(the_url, _
                    Len(the_url) - pos + 1)

                ‘ Remove the file from the URL.
                the_url = Left$(the_url, pos - 1)
            Else
                ‘ No “.”. There is no file.
                the_file = NO_FILE
            End If
        End If
    End If

    ‘ Whatever is left is the path.
    If the_url = “” Then
        ‘ There is no path.
        the_path = NO_PATH
    Else
        the_path = the_url

        ‘ If we got a site, make sure there’s
        ‘ a leading slash.
        If got_site And Left$(the_path, 1) <> “/” Then
            the_path = “/” & the_path
        End If

        ‘ Make sure there’s a trailing slash.
        If Right$(the_path, 1) <> “/” Then
            the_path = the_path & “/”
        End If
    End If
End Sub


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.