Back to index

lightning-sunbird  0.9+nobinonly
Public Member Functions | Public Attributes | Protected Member Functions | Private Member Functions | Private Attributes
nsDefaultURIFixup Class Reference

#include <nsDefaultURIFixup.h>

Inheritance diagram for nsDefaultURIFixup:
Inheritance graph
[legend]
Collaboration diagram for nsDefaultURIFixup:
Collaboration graph
[legend]

List of all members.

Public Member Functions

NS_DECL_ISUPPORTS
NS_DECL_NSIURIFIXUP
NS_DECL_NSIURIFIXUP_MOZILLA_1_8_BRANCH 
nsDefaultURIFixup ()
nsIURI keywordToURI (in AUTF8String aKeyword)
 Converts the specified keyword string into a URI.
nsIURI createExposableURI (in nsIURI aURI)
 Converts an internal URI (e.g.
nsIURI createFixupURI (in AUTF8String aURIText, in unsigned long aFixupFlags)
 Converts the specified string into a URI, first attempting to correct any errors in the syntax or other vagaries.

Public Attributes

const unsigned long FIXUP_FLAG_NONE = 0
 No fixup flags.
const unsigned long FIXUP_FLAG_ALLOW_KEYWORD_LOOKUP = 1
 Allow the fixup to use a keyword lookup service to complete the URI.
const unsigned long FIXUP_FLAGS_MAKE_ALTERNATE_URI = 2
 Tell the fixup to make an alternate URI from the input URI, for example to turn foo into www.foo.com.

Protected Member Functions

virtual ~nsDefaultURIFixup ()

Private Member Functions

nsresult FileURIFixup (const nsACString &aStringURI, nsIURI **aURI)
nsresult ConvertFileToStringURI (const nsACString &aIn, nsCString &aOut)
nsresult KeywordURIFixup (const nsACString &aStringURI, nsIURI **aURI)
PRBool PossiblyByteExpandedFileName (const nsAString &aIn)
PRBool PossiblyHostPortUrl (const nsACString &aUrl)
PRBool MakeAlternateURI (nsIURI *aURI)
const char * GetFileSystemCharset ()
const char * GetCharsetForUrlBar ()

Private Attributes

nsCOMPtr< nsIPrefBranchmPrefBranch
nsCAutoString mFsCharset

Detailed Description

Definition at line 51 of file nsDefaultURIFixup.h.


Constructor & Destructor Documentation

Definition at line 58 of file nsDefaultURIFixup.cpp.

{
  /* member initializers and constructor code */

  // Try and get the pref service
  mPrefBranch = do_GetService(NS_PREFSERVICE_CONTRACTID);
}

Here is the call graph for this function:

nsDefaultURIFixup::~nsDefaultURIFixup ( ) [protected, virtual]

Definition at line 67 of file nsDefaultURIFixup.cpp.

{
  /* destructor code */
}

Member Function Documentation

nsresult nsDefaultURIFixup::ConvertFileToStringURI ( const nsACString &  aIn,
nsCString aOut 
) [private]

Definition at line 524 of file nsDefaultURIFixup.cpp.

{
    PRBool attemptFixup = PR_FALSE;

#if defined(XP_WIN) || defined(XP_OS2)
    // Check for \ in the url-string or just a drive (PC)
    if(kNotFound != aIn.FindChar('\\') ||
       (aIn.Length() == 2 && (aIn.Last() == ':' || aIn.Last() == '|')))
    {
        attemptFixup = PR_TRUE;
    }
#elif defined(XP_UNIX) || defined(XP_BEOS)
    // Check if it starts with / (UNIX)
    if(aIn.First() == '/')
    {
        attemptFixup = PR_TRUE;
    }
#else
    // Do nothing (All others for now) 
#endif

    if (attemptFixup)
    {
        // Test if this is a valid path by trying to create a local file
        // object. The URL of that is returned if successful.

        // NOTE: Please be sure to check that the call to NS_NewLocalFile
        //       rejects bad file paths when using this code on a new
        //       platform.

        nsCOMPtr<nsILocalFile> filePath;
        nsresult rv;

        // this is not the real fix but a temporary fix
        // in order to really fix the problem, we need to change the 
        // nsICmdLineService interface to use wstring to pass paramenters 
        // instead of string since path name and other argument could be
        // in non ascii.(see bug 87127) Since it is too risky to make interface change right
        // now, we decide not to do so now.
        // Therefore, the aIn we receive here maybe already in damage form
        // (e.g. treat every bytes as ISO-8859-1 and cast up to PRUnichar
        //  while the real data could be in file system charset )
        // we choice the following logic which will work for most of the case.
        // Case will still failed only if it meet ALL the following condiction:
        //    1. running on CJK, Russian, or Greek system, and 
        //    2. user type it from URL bar
        //    3. the file name contains character in the range of 
        //       U+00A1-U+00FF but encode as different code point in file
        //       system charset (e.g. ACP on window)- this is very rare case
        // We should remove this logic and convert to File system charset here
        // once we change nsICmdLineService to use wstring and ensure
        // all the Unicode data come in is correctly converted.
        // XXXbz nsICmdLineService doesn't hand back unicode, so in some cases
        // what we have is actually a "utf8" version of a "utf16" string that's
        // actually byte-expanded native-encoding data.  Someone upstream needs
        // to stop using AssignWithConversion and do things correctly.  See bug
        // 58866 for what happens if we remove this
        // PossiblyByteExpandedFileName check.
        NS_ConvertUTF8toUCS2 in(aIn);
        if (PossiblyByteExpandedFileName(in)) {
          // removes high byte
          rv = NS_NewNativeLocalFile(NS_LossyConvertUCS2toASCII(in), PR_FALSE, getter_AddRefs(filePath));
        }
        else {
          // input is unicode
          rv = NS_NewLocalFile(in, PR_FALSE, getter_AddRefs(filePath));
        }

        if (NS_SUCCEEDED(rv))
        {
            NS_GetURLSpecFromFile(filePath, aOut);
            return NS_OK;
        }
    }

    return NS_ERROR_FAILURE;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Converts an internal URI (e.g.

a wyciwyg URI) into one which we can expose to the user, for example on the URL bar.

Parameters:
aURIThe URI to be converted
Returns:
nsIURI The converted, exposable URI
Exceptions:
NS_ERROR_MALFORMED_URIwhen the exposable portion of aURI is malformed
NS_ERROR_UNKNOWN_PROTOCOLwhen we can't get a protocol handler service for the URI scheme.
nsIURI nsIURIFixup::createFixupURI ( in AUTF8String  aURIText,
in unsigned long  aFixupFlags 
) [inherited]

Converts the specified string into a URI, first attempting to correct any errors in the syntax or other vagaries.

Returns a wellformed URI or nsnull if it can't.

Parameters:
aURITextCandidate URI.
aFixupFlagsFlags that govern ways the URI may be fixed up.
nsresult nsDefaultURIFixup::FileURIFixup ( const nsACString &  aStringURI,
nsIURI **  aURI 
) [private]

Definition at line 509 of file nsDefaultURIFixup.cpp.

{
    nsCAutoString uriSpecOut;

    nsresult rv = ConvertFileToStringURI(aStringURI, uriSpecOut);
    if (NS_SUCCEEDED(rv))
    {
        // if this is file url, uriSpecOut is already in FS charset
        if(NS_SUCCEEDED(NS_NewURI(aURI, uriSpecOut.get(), nsnull)))
            return NS_OK;
    } 
    return NS_ERROR_FAILURE;
}

Here is the call graph for this function:

Definition at line 743 of file nsDefaultURIFixup.cpp.

{
  const char *charset = GetFileSystemCharset();
#ifdef XP_MAC
  // check for "x-mac-" prefix
  if ((strlen(charset) >= 6) && charset[0] == 'x' && charset[2] == 'm')
  {
    if (!strcmp("x-mac-roman", charset))
      return "ISO-8859-1";
    // we can do more x-mac-xxxx mapping here
    // or somewhere in intl code like nsIPlatformCharset.
  }
#endif
  return charset;
}

Here is the call graph for this function:

Definition at line 724 of file nsDefaultURIFixup.cpp.

{
  if (mFsCharset.IsEmpty())
  {
    nsresult rv;
    nsCAutoString charset;
    nsCOMPtr<nsIPlatformCharset> plat(do_GetService(NS_PLATFORMCHARSET_CONTRACTID, &rv));
    if (NS_SUCCEEDED(rv))
      rv = plat->GetCharset(kPlatformCharsetSel_FileName, charset);

    if (charset.IsEmpty())
      mFsCharset.AssignLiteral("ISO-8859-1");
    else
      mFsCharset.Assign(charset);
  }

  return mFsCharset.get();
}

Here is the call graph for this function:

Here is the caller graph for this function:

nsIURI nsIURIFixup_MOZILLA_1_8_BRANCH::keywordToURI ( in AUTF8String  aKeyword) [inherited]

Converts the specified keyword string into a URI.

Note that it's the caller's responsibility to check whether keywords are enabled and whether aKeyword is a sensible keyword.

nsresult nsDefaultURIFixup::KeywordURIFixup ( const nsACString &  aStringURI,
nsIURI **  aURI 
) [private]

Definition at line 759 of file nsDefaultURIFixup.cpp.

{
    // These are keyword formatted strings
    // "what is mozilla"
    // "what is mozilla?"
    // "docshell site:mozilla.org" - has no dot/colon in the first space-separated substring
    // "?mozilla" - anything that begins with a question mark
    // "?site:mozilla.org docshell"

    // These are not keyword formatted strings
    // "www.blah.com" - first space-separated substring contains a dot, doesn't start with "?"
    // "www.blah.com stuff"
    // "nonQualifiedHost:80" - first space-separated substring contains a colon, doesn't start with "?"
    // "nonQualifiedHost:80 args"
    // "nonQualifiedHost?"
    // "nonQualifiedHost?args"
    // "nonQualifiedHost?some args"

    PRInt32 dotLoc   = aURIString.FindChar('.');
    PRInt32 colonLoc = aURIString.FindChar(':');
    PRInt32 spaceLoc = aURIString.FindChar(' ');
    PRInt32 qMarkLoc = aURIString.FindChar('?');

    if ((dotLoc == kNotFound || (spaceLoc > 0 && spaceLoc < dotLoc)) &&
        (colonLoc == kNotFound || (spaceLoc > 0 && spaceLoc < colonLoc)) &&
        (spaceLoc > 0 && (qMarkLoc == kNotFound || spaceLoc < qMarkLoc)) ||
        qMarkLoc == 0)
    {
        KeywordToURI(aURIString, aURI);
    }

    if(*aURI)
        return NS_OK;

    return NS_ERROR_FAILURE;
}

Definition at line 406 of file nsDefaultURIFixup.cpp.

{
    if (!mPrefBranch)
    {
        return PR_FALSE;
    }
    PRBool makeAlternate = PR_TRUE;
    mPrefBranch->GetBoolPref("browser.fixup.alternate.enabled", &makeAlternate);
    if (!makeAlternate)
    {
        return PR_FALSE;
    }

    // Code only works for http. Not for any other protocol including https!
    PRBool isHttp = PR_FALSE;
    aURI->SchemeIs("http", &isHttp);
    if (!isHttp) {
        return PR_FALSE;
    }

    // Security - URLs with user / password info should NOT be fixed up
    nsCAutoString userpass;
    aURI->GetUserPass(userpass);
    if (!userpass.IsEmpty()) {
        return PR_FALSE;
    }

    nsCAutoString oldHost;
    nsCAutoString newHost;
    aURI->GetHost(oldHost);

    // Count the dots
    PRInt32 numDots = 0;
    nsReadingIterator<char> iter;
    nsReadingIterator<char> iterEnd;
    oldHost.BeginReading(iter);
    oldHost.EndReading(iterEnd);
    while (iter != iterEnd) {
        if (*iter == '.')
            numDots++;
        ++iter;
    }


    nsresult rv;

    // Get the prefix and suffix to stick onto the new hostname. By default these
    // are www. & .com but they could be any other value, e.g. www. & .org

    nsCAutoString prefix("www.");
    nsXPIDLCString prefPrefix;
    rv = mPrefBranch->GetCharPref("browser.fixup.alternate.prefix", getter_Copies(prefPrefix));
    if (NS_SUCCEEDED(rv))
    {
        prefix.Assign(prefPrefix);
    }

    nsCAutoString suffix(".com");
    nsXPIDLCString prefSuffix;
    rv = mPrefBranch->GetCharPref("browser.fixup.alternate.suffix", getter_Copies(prefSuffix));
    if (NS_SUCCEEDED(rv))
    {
        suffix.Assign(prefSuffix);
    }
    
    if (numDots == 0)
    {
        newHost.Assign(prefix);
        newHost.Append(oldHost);
        newHost.Append(suffix);
    }
    else if (numDots == 1)
    {
        if (!prefix.IsEmpty() &&
                oldHost.EqualsIgnoreCase(prefix.get(), prefix.Length())) {
            newHost.Assign(oldHost);
            newHost.Append(suffix);
        }
        else if (!suffix.IsEmpty()) {
            newHost.Assign(prefix);
            newHost.Append(oldHost);
        }
        else
        {
            // Do nothing
            return PR_FALSE;
        }
    }
    else
    {
        // Do nothing
        return PR_FALSE;
    }

    if (newHost.IsEmpty()) {
        return PR_FALSE;
    }

    // Assign the new host string over the old one
    aURI->SetHost(newHost);
    return PR_TRUE;
}

Here is the call graph for this function:

Definition at line 703 of file nsDefaultURIFixup.cpp.

{
    // XXXXX HACK XXXXX : please don't copy this code.
    // There are cases where aIn contains the locale byte chars padded to short
    // (thus the name "ByteExpanded"); whereas other cases 
    // have proper Unicode code points.
    // This is a temporary fix.  Please refer to 58866, 86948

    nsReadingIterator<PRUnichar> iter;
    nsReadingIterator<PRUnichar> iterEnd;
    aIn.BeginReading(iter);
    aIn.EndReading(iterEnd);
    while (iter != iterEnd)
    {
        if (*iter >= 0x0080 && *iter <= 0x00FF)
            return PR_TRUE;
        ++iter;
    }
    return PR_FALSE;
}

Here is the caller graph for this function:

PRBool nsDefaultURIFixup::PossiblyHostPortUrl ( const nsACString &  aUrl) [private]

Definition at line 603 of file nsDefaultURIFixup.cpp.

{
    // Oh dear, the protocol is invalid. Test if the protocol might
    // actually be a url without a protocol:
    //
    //   http://www.faqs.org/rfcs/rfc1738.html
    //   http://www.faqs.org/rfcs/rfc2396.html
    //
    // e.g. Anything of the form:
    //
    //   <hostname>:<port> or
    //   <hostname>:<port>/
    //
    // Where <hostname> is a string of alphanumeric characters and dashes
    // separated by dots.
    // and <port> is a 5 or less digits. This actually breaks the rfc2396
    // definition of a scheme which allows dots in schemes.
    //
    // Note:
    //   People expecting this to work with
    //   <user>:<password>@<host>:<port>/<url-path> will be disappointed!
    //
    // Note: Parser could be a lot tighter, tossing out silly hostnames
    //       such as those containing consecutive dots and so on.

    // Read the hostname which should of the form
    // [a-zA-Z0-9\-]+(\.[a-zA-Z0-9\-]+)*:

    nsACString::const_iterator iterBegin;
    nsACString::const_iterator iterEnd;
    aUrl.BeginReading(iterBegin);
    aUrl.EndReading(iterEnd);
    nsACString::const_iterator iter = iterBegin;

    while (iter != iterEnd)
    {
        PRUint32 chunkSize = 0;
        // Parse a chunk of the address
        while (iter != iterEnd &&
               (*iter == '-' ||
                nsCRT::IsAsciiAlpha(*iter) ||
                nsCRT::IsAsciiDigit(*iter)))
        {
            ++chunkSize;
            ++iter;
        }
        if (chunkSize == 0 || iter == iterEnd)
        {
            return PR_FALSE;
        }
        if (*iter == ':')
        {
            // Go onto checking the for the digits
            break;
        }
        if (*iter != '.')
        {
            // Whatever it is, it ain't a hostname!
            return PR_FALSE;
        }
        ++iter;
    }
    if (iter == iterEnd)
    {
        // No point continuing since there is no colon
        return PR_FALSE;
    }
    ++iter;

    // Count the number of digits after the colon and before the
    // next forward slash (or end of string)

    PRUint32 digitCount = 0;
    while (iter != iterEnd && digitCount <= 5)
    {
        if (nsCRT::IsAsciiDigit(*iter))
        {
            digitCount++;
        }
        else if (*iter == '/')
        {
            break;
        }
        else
        {
            // Whatever it is, it ain't a port!
            return PR_FALSE;
        }
        ++iter;
    }
    if (digitCount == 0 || digitCount > 5)
    {
        // No digits or more digits than a port would have.
        return PR_FALSE;
    }

    // Yes, it's possibly a host:port url
    return PR_TRUE;
}

Member Data Documentation

Allow the fixup to use a keyword lookup service to complete the URI.

The fixup object implementer should honour this flag and only perform any lengthy keyword (or search) operation if it is set.

Definition at line 58 of file nsIURIFixup.idl.

const unsigned long nsIURIFixup::FIXUP_FLAG_NONE = 0 [inherited]

No fixup flags.

Definition at line 51 of file nsIURIFixup.idl.

Tell the fixup to make an alternate URI from the input URI, for example to turn foo into www.foo.com.

Definition at line 64 of file nsIURIFixup.idl.

Definition at line 75 of file nsDefaultURIFixup.h.

Definition at line 74 of file nsDefaultURIFixup.h.


The documentation for this class was generated from the following files: