FREE Subscription to Dr. Dobb’s Digest: Same Great Content, New Digital Edition
Site Archive (Complete)
Dobbs M-Dev
Email
Print
Reprint

add to:
Del.icio.us
Digg
Google
Furl
Slashdot
Y! MyWeb
Blink
October 01, 2003

URL Canonicalization Testing

(Page 2 of 16)
URL Canonicalization Testing

Listing 1 CNavigationInjector


// from header:
// std::vector<std::wstring> paths;
// const std::wstring pathSeparatorCharacters;

CNavigationInjector::CNavigationInjector(std::wstring path)
    : pathSeparatorCharacters(L"/\\")
    {
    // We should have been given just the path, but we'll run it through a Url
    //    just to be sure.
    CUrl url(path);
    if (0 < url.Path().length())
        {
        unsigned long injectionPoint(SelectInjectionPoint(url.Path()));
        InjectNavigationAtPoint(url.Path(), injectionPoint);
        }
    }
void CNavigationInjector::InjectNavigationAtPoint(std::wstring path
    , unsigned long injectionPoint)
    {
    InjectForwardSlashDot(path, injectionPoint);
    InjectForwardSlashDotDot(path, injectionPoint);
    }
void CNavigationInjector::InjectForwardSlashDot(std::wstring path
    , unsigned long injectionPoint)
    {
    paths.push_back(path.insert(injectionPoint + 1, L"./"));
    }
void CNavigationInjector::InjectForwardSlashDotDot(std::wstring path
    , unsigned long injectionPoint)
    {
    std::wstring parentFolder(GetParentFolder(path, injectionPoint));
    if (0 < parentFolder.length())
        {
        paths.push_back(path.insert(injectionPoint + 1, L"../" 	+ parentFolder));
        }
    }
unsigned long CNavigationInjector::SelectInjectionPoint(std::wstring path) const
    {
    // Count the number of path separator characters in the path.
    InjectionPointList possibilities;
    std::wstring::size_type location(0);
    while (std::wstring::npos != 
        (location = path.find_first_of(pathSeparatorCharacters, location)))
        {
        possibilities.push_back(location);
        ++location;
        }

    // Pick one of the locations as the injection point.  We only pick a 
    // random point if there are more than two path separators found (and
    // don't consider the first location as a possibility) to ensure we can 
    // construct a valid path when ".." is injected.
    if (2 < possibilities.size())
        {
        unsigned long index(SelectRandomIndex(2, possibilities.size()));
        unsigned long position(possibilities[index - 1]);
        return position;
        }
    else if (2 == possibilities.size())
        {
        //-------------------------------------------------
        // If we found two path separators the first one should be the root
        // and the second one should be trailing the top-level folder; it's
        // this end location we want to modify.
        return possibilities[1];
        }
    else 
        {
        //-------------------------------------------------
        // There are either zero or one path separators found; the first should
        // be the root, so we return position zero either way.
        return 0;
        }
    }

std::wstring CNavigationInjector::GetParentFolder(std::wstring path
    , unsigned long childPathStartPoint) const
    {
    std::wstring::size_type parentPathStartPoint(path.find_last_of(
        pathSeparatorCharacters, (childPathStartPoint - 1)));
    if (std::wstring::npos != parentPathStartPoint)
        {
        return path.substr(parentPathStartPoint + 1
            , (childPathStartPoint - parentPathStartPoint));
        }
    else
        {
        return L"";
        }
    }

Previous Page | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 Next Page
TOP 5 ARTICLES
No Top Articles.



MICROSITES
FEATURED TOPIC

ADDITIONAL TOPICS

INFO-LINK