2000-11-09 00:00:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public
|
|
|
|
* License Version 1.1 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS
|
|
|
|
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
|
|
|
* implied. See the License for the specific language governing
|
|
|
|
* rights and limitations under the License.
|
|
|
|
*
|
|
|
|
* The Original Code is the Mozilla browser.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Netscape
|
|
|
|
* Communications, Inc. Portions created by Netscape are
|
|
|
|
* Copyright (C) 1999, Mozilla. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Adam Lock <adamlock@netscape.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "nsString.h"
|
2001-09-29 12:28:41 +04:00
|
|
|
#include "nsReadableUtils.h"
|
2000-11-09 00:00:37 +03:00
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsEscape.h"
|
|
|
|
|
|
|
|
#include "nsICharsetConverterManager.h"
|
|
|
|
#include "nsIPlatformCharset.h"
|
2001-05-17 23:24:42 +04:00
|
|
|
#include "nsILocalFile.h"
|
2000-11-09 00:00:37 +03:00
|
|
|
|
|
|
|
#include "nsIURIFixup.h"
|
|
|
|
#include "nsDefaultURIFixup.h"
|
|
|
|
|
|
|
|
static NS_DEFINE_CID(kPlatformCharsetCID, NS_PLATFORMCHARSET_CID);
|
|
|
|
static NS_DEFINE_CID(kCharsetConverterManagerCID, NS_ICHARSETCONVERTERMANAGER_CID);
|
|
|
|
|
|
|
|
/* Implementation file */
|
|
|
|
NS_IMPL_ISUPPORTS1(nsDefaultURIFixup, nsIURIFixup)
|
|
|
|
|
|
|
|
nsDefaultURIFixup::nsDefaultURIFixup()
|
|
|
|
{
|
|
|
|
NS_INIT_ISUPPORTS();
|
|
|
|
/* member initializers and constructor code */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsDefaultURIFixup::~nsDefaultURIFixup()
|
|
|
|
{
|
|
|
|
/* destructor code */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-11-07 01:55:30 +03:00
|
|
|
/* nsIURI createFixupURI (in wstring aURIText, in unsigned long aFixupFlags); */
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDefaultURIFixup::CreateFixupURI(const PRUnichar *aStringURI, PRUint32 aFixupFlags, nsIURI **aURI)
|
2000-11-09 00:00:37 +03:00
|
|
|
{
|
2001-11-07 01:55:30 +03:00
|
|
|
NS_ENSURE_ARG_POINTER(aStringURI);
|
|
|
|
NS_ENSURE_ARG_POINTER(aURI);
|
|
|
|
|
2000-11-09 00:00:37 +03:00
|
|
|
*aURI = nsnull;
|
|
|
|
|
|
|
|
// Try and get the prefs service
|
|
|
|
if (!mPrefs)
|
|
|
|
{
|
|
|
|
mPrefs = do_GetService(NS_PREF_CONTRACTID);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString uriString(aStringURI);
|
|
|
|
uriString.Trim(" "); // Cleanup the empty spaces that might be on each end.
|
|
|
|
|
|
|
|
// Eliminate embedded newlines, which single-line text fields now allow:
|
|
|
|
uriString.StripChars("\r\n");
|
|
|
|
|
2001-11-07 01:55:30 +03:00
|
|
|
// View-source is a pseudo scheme. We're interested in fixing up the stuff
|
|
|
|
// after it. The easiest way to do that is to call this method again with the
|
|
|
|
// "view-source:" lopped off and then prepend it again afterwards.
|
2000-11-09 00:00:37 +03:00
|
|
|
|
2001-11-07 01:55:30 +03:00
|
|
|
if (uriString.EqualsIgnoreCase("view-source:", 12))
|
2001-05-17 23:24:42 +04:00
|
|
|
{
|
2001-11-07 01:55:30 +03:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
2001-11-07 02:06:36 +03:00
|
|
|
PRUint32 newFixupFlags = aFixupFlags & ~FIXUP_FLAG_ALLOW_KEYWORD_LOOKUP;
|
2001-11-07 22:03:39 +03:00
|
|
|
|
|
|
|
nsAutoString tempString;
|
|
|
|
tempString = Substring(uriString, 12, uriString.Length() - 12);
|
|
|
|
nsresult rv = CreateFixupURI(tempString.get(),
|
|
|
|
newFixupFlags,
|
|
|
|
getter_AddRefs(uri));
|
2001-11-07 01:55:30 +03:00
|
|
|
if (NS_FAILED(rv) || !uri)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
nsXPIDLCString spec;
|
|
|
|
uri->GetSpec(getter_Copies(spec));
|
|
|
|
uriString.Assign(NS_LITERAL_STRING("view-source:"));
|
|
|
|
uriString.Append(NS_ConvertASCIItoUCS2(spec));
|
2001-05-17 23:24:42 +04:00
|
|
|
}
|
2001-11-07 01:55:30 +03:00
|
|
|
else {
|
|
|
|
// Check for if it is a file URL
|
|
|
|
FileURIFixup(uriString.get(), aURI);
|
|
|
|
if(*aURI)
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
#ifdef XP_PC
|
|
|
|
// Not a file URL, so translate '\' to '/' for convenience in the common protocols
|
|
|
|
if (uriString.FindChar(':') == -1 ||
|
|
|
|
uriString.EqualsIgnoreCase("http:", 5) ||
|
|
|
|
uriString.EqualsIgnoreCase("https:", 6) ||
|
|
|
|
uriString.EqualsIgnoreCase("ftp:", 4))
|
|
|
|
{
|
|
|
|
uriString.ReplaceChar(PRUnichar('\\'), PRUnichar('/'));
|
|
|
|
}
|
2001-05-17 23:24:42 +04:00
|
|
|
#endif
|
2001-11-07 01:55:30 +03:00
|
|
|
}
|
2001-05-17 23:24:42 +04:00
|
|
|
|
2001-05-10 06:40:18 +04:00
|
|
|
// Just try to create an URL out of it
|
|
|
|
NS_NewURI(aURI, uriString, nsnull);
|
2000-11-09 00:00:37 +03:00
|
|
|
if(*aURI)
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
// See if it is a keyword
|
|
|
|
// Test whether keywords need to be fixed up
|
2001-11-07 01:55:30 +03:00
|
|
|
if (aFixupFlags & FIXUP_FLAG_ALLOW_KEYWORD_LOOKUP) {
|
|
|
|
PRBool fixupKeywords = PR_FALSE;
|
|
|
|
if (mPrefs)
|
|
|
|
{
|
|
|
|
NS_ENSURE_SUCCESS(mPrefs->GetBoolPref("keyword.enabled", &fixupKeywords), NS_ERROR_FAILURE);
|
|
|
|
}
|
|
|
|
if (fixupKeywords)
|
|
|
|
{
|
|
|
|
KeywordURIFixup(uriString.get(), aURI);
|
|
|
|
if(*aURI)
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2000-11-09 00:00:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// See if a protocol needs to be added
|
|
|
|
PRInt32 checkprotocol = uriString.Find("://",0);
|
|
|
|
// if no scheme (protocol) is found, assume http or ftp.
|
|
|
|
if (checkprotocol == -1) {
|
|
|
|
// find host name
|
|
|
|
PRInt32 hostPos = uriString.FindCharInSet("./:");
|
|
|
|
if (hostPos == -1)
|
|
|
|
hostPos = uriString.Length();
|
|
|
|
|
|
|
|
// extract host name
|
|
|
|
nsAutoString hostSpec;
|
|
|
|
uriString.Left(hostSpec, hostPos);
|
|
|
|
|
|
|
|
// insert url spec corresponding to host name
|
|
|
|
if (hostSpec.EqualsIgnoreCase("ftp"))
|
|
|
|
uriString.InsertWithConversion("ftp://", 0, 6);
|
|
|
|
else
|
|
|
|
uriString.InsertWithConversion("http://", 0, 7);
|
|
|
|
} // end if checkprotocol
|
|
|
|
|
|
|
|
return NS_NewURI(aURI, uriString, nsnull);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-12-20 02:01:25 +03:00
|
|
|
nsresult nsDefaultURIFixup::FileURIFixup(const PRUnichar* aStringURI,
|
|
|
|
nsIURI** aURI)
|
2000-11-09 00:00:37 +03:00
|
|
|
{
|
|
|
|
nsAutoString uriSpecIn(aStringURI);
|
2001-06-22 05:28:18 +04:00
|
|
|
nsCAutoString uriSpecOut;
|
2000-11-09 00:00:37 +03:00
|
|
|
|
2001-05-17 23:24:42 +04:00
|
|
|
nsresult rv = ConvertFileToStringURI(uriSpecIn, uriSpecOut);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
2000-11-09 00:00:37 +03:00
|
|
|
{
|
2001-06-22 05:28:18 +04:00
|
|
|
// if this is file url, uriSpecOut is already in FS charset
|
|
|
|
if(NS_SUCCEEDED(NS_NewURI(aURI, uriSpecOut.get(), nsnull)))
|
2000-11-09 00:00:37 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2000-12-20 02:01:25 +03:00
|
|
|
nsresult nsDefaultURIFixup::ConvertFileToStringURI(nsString& aIn,
|
2001-06-22 05:28:18 +04:00
|
|
|
nsCString& aOut)
|
2000-11-09 00:00:37 +03:00
|
|
|
{
|
2001-05-17 23:24:42 +04:00
|
|
|
PRBool attemptFixup = PR_FALSE;
|
|
|
|
|
2000-11-09 00:00:37 +03:00
|
|
|
#ifdef XP_PC
|
|
|
|
// Check for \ in the url-string or just a drive (PC)
|
|
|
|
if(kNotFound != aIn.FindChar(PRUnichar('\\')) || ((aIn.Length() == 2 ) && (aIn.Last() == PRUnichar(':') || aIn.Last() == PRUnichar('|'))))
|
|
|
|
{
|
2001-05-17 23:24:42 +04:00
|
|
|
attemptFixup = PR_TRUE;
|
|
|
|
}
|
2000-11-09 00:00:37 +03:00
|
|
|
#elif XP_UNIX
|
|
|
|
// Check if it starts with / or \ (UNIX)
|
2001-06-30 15:02:25 +04:00
|
|
|
const PRUnichar * up = aIn.get();
|
2000-11-09 00:00:37 +03:00
|
|
|
if((PRUnichar('/') == *up) || (PRUnichar('\\') == *up))
|
|
|
|
{
|
2001-05-17 23:24:42 +04:00
|
|
|
attemptFixup = PR_TRUE;
|
|
|
|
}
|
2000-11-09 00:00:37 +03:00
|
|
|
#else
|
2001-05-17 23:24:42 +04:00
|
|
|
// Do nothing (All others for now)
|
2000-11-09 00:00:37 +03:00
|
|
|
#endif
|
|
|
|
|
2001-05-17 23:24:42 +04:00
|
|
|
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;
|
2001-06-22 05:28:18 +04:00
|
|
|
nsCAutoString file;
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
if (PossiblyByteExpandedFileName(aIn)) {
|
|
|
|
// removes high byte
|
|
|
|
file.AssignWithConversion(aIn);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// converts from Unicode to FS Charset
|
|
|
|
ConvertStringURIToFileCharset(aIn, file);
|
|
|
|
}
|
|
|
|
|
2001-10-27 11:43:56 +04:00
|
|
|
nsresult rv = NS_NewLocalFile(file.get(), PR_FALSE, getter_AddRefs(filePath));
|
2001-05-17 23:24:42 +04:00
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
{
|
|
|
|
nsXPIDLCString fileurl;
|
|
|
|
filePath->GetURL(getter_Copies(fileurl));
|
2001-06-22 05:28:18 +04:00
|
|
|
aOut.Assign(fileurl);
|
2001-05-17 23:24:42 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2000-11-09 00:00:37 +03:00
|
|
|
}
|
|
|
|
|
2001-05-17 23:24:42 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2000-11-09 00:00:37 +03:00
|
|
|
}
|
|
|
|
|
2001-06-22 05:28:18 +04:00
|
|
|
PRBool nsDefaultURIFixup::PossiblyByteExpandedFileName(nsString& aIn)
|
|
|
|
{
|
|
|
|
// 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
|
|
|
|
const PRUnichar* uniChar = aIn.get();
|
|
|
|
for (PRUint32 i = 0; i < aIn.Length(); i++) {
|
|
|
|
if ((uniChar[i] >= 0x0080) && (uniChar[i] <= 0x00FF)) {
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
2000-11-09 00:00:37 +03:00
|
|
|
|
2000-12-20 02:01:25 +03:00
|
|
|
nsresult nsDefaultURIFixup::ConvertStringURIToFileCharset(nsString& aIn,
|
|
|
|
nsCString& aOut)
|
2000-11-09 00:00:37 +03:00
|
|
|
{
|
|
|
|
aOut = "";
|
|
|
|
// for file url, we need to convert the nsString to the file system
|
|
|
|
// charset before we pass to NS_NewURI
|
|
|
|
static nsAutoString fsCharset;
|
|
|
|
// find out the file system charset first
|
|
|
|
if(0 == fsCharset.Length())
|
|
|
|
{
|
|
|
|
fsCharset.AssignWithConversion("ISO-8859-1"); // set the fallback first.
|
|
|
|
nsCOMPtr<nsIPlatformCharset> plat(do_GetService(kPlatformCharsetCID));
|
|
|
|
NS_ENSURE_TRUE(plat, NS_ERROR_FAILURE);
|
|
|
|
NS_ENSURE_SUCCESS(plat->GetCharset(kPlatformCharsetSel_FileName, fsCharset),
|
|
|
|
NS_ERROR_FAILURE);
|
|
|
|
}
|
|
|
|
// We probably should cache ccm here.
|
|
|
|
// get a charset converter from the manager
|
|
|
|
nsCOMPtr<nsICharsetConverterManager> ccm(do_GetService(kCharsetConverterManagerCID));
|
|
|
|
NS_ENSURE_TRUE(ccm, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIUnicodeEncoder> fsEncoder;
|
|
|
|
NS_ENSURE_SUCCESS(ccm->GetUnicodeEncoder(&fsCharset,
|
|
|
|
getter_AddRefs(fsEncoder)), NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
PRInt32 bufLen = 0;
|
2001-06-30 15:02:25 +04:00
|
|
|
NS_ENSURE_SUCCESS(fsEncoder->GetMaxLength(aIn.get(), aIn.Length(),
|
2000-11-09 00:00:37 +03:00
|
|
|
&bufLen), NS_ERROR_FAILURE);
|
|
|
|
aOut.SetCapacity(bufLen+1);
|
|
|
|
PRInt32 srclen = aIn.Length();
|
2001-06-30 15:02:25 +04:00
|
|
|
NS_ENSURE_SUCCESS(fsEncoder->Convert(aIn.get(), &srclen,
|
2001-03-02 12:26:57 +03:00
|
|
|
(char*)aOut.get(), &bufLen), NS_ERROR_FAILURE);
|
2000-11-09 00:00:37 +03:00
|
|
|
|
2001-03-02 12:26:57 +03:00
|
|
|
((char*)aOut.get())[bufLen]='\0';
|
2000-11-09 00:00:37 +03:00
|
|
|
aOut.SetLength(bufLen);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-12-20 02:01:25 +03:00
|
|
|
nsresult nsDefaultURIFixup::KeywordURIFixup(const PRUnichar* aStringURI,
|
|
|
|
nsIURI** aURI)
|
2000-11-09 00:00:37 +03:00
|
|
|
{
|
|
|
|
// These are keyword formatted strings
|
|
|
|
// "what is mozilla"
|
|
|
|
// "what is mozilla?"
|
|
|
|
// "?mozilla"
|
|
|
|
// "?What is mozilla"
|
|
|
|
|
|
|
|
// These are not keyword formatted strings
|
|
|
|
// "www.blah.com" - anything with a dot in it
|
|
|
|
// "nonQualifiedHost:80" - anything with a colon in it
|
|
|
|
// "nonQualifiedHost?"
|
|
|
|
// "nonQualifiedHost?args"
|
|
|
|
// "nonQualifiedHost?some args"
|
|
|
|
|
|
|
|
nsAutoString uriString(aStringURI);
|
|
|
|
if(uriString.FindChar('.') == -1 && uriString.FindChar(':') == -1)
|
|
|
|
{
|
|
|
|
PRInt32 qMarkLoc = uriString.FindChar('?');
|
|
|
|
PRInt32 spaceLoc = uriString.FindChar(' ');
|
|
|
|
|
|
|
|
PRBool keyword = PR_FALSE;
|
|
|
|
if(qMarkLoc == 0)
|
|
|
|
keyword = PR_TRUE;
|
|
|
|
else if((spaceLoc > 0) && ((qMarkLoc == -1) || (spaceLoc < qMarkLoc)))
|
|
|
|
keyword = PR_TRUE;
|
|
|
|
|
|
|
|
if(keyword)
|
|
|
|
{
|
|
|
|
nsCAutoString keywordSpec("keyword:");
|
2001-09-29 12:28:41 +04:00
|
|
|
char *utf8Spec = ToNewUTF8String(uriString);
|
2000-11-09 00:00:37 +03:00
|
|
|
if(utf8Spec)
|
|
|
|
{
|
|
|
|
char* escapedUTF8Spec = nsEscape(utf8Spec, url_Path);
|
|
|
|
if(escapedUTF8Spec)
|
|
|
|
{
|
|
|
|
keywordSpec.Append(escapedUTF8Spec);
|
2001-03-02 12:26:57 +03:00
|
|
|
NS_NewURI(aURI, keywordSpec.get(), nsnull);
|
2000-11-09 00:00:37 +03:00
|
|
|
nsMemory::Free(escapedUTF8Spec);
|
|
|
|
} // escapedUTF8Spec
|
|
|
|
nsMemory::Free(utf8Spec);
|
|
|
|
} // utf8Spec
|
|
|
|
} // keyword
|
|
|
|
} // FindChar
|
|
|
|
|
|
|
|
if(*aURI)
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult NS_NewURIFixup(nsIURIFixup **aURIFixup)
|
|
|
|
{
|
|
|
|
nsDefaultURIFixup *fixup = new nsDefaultURIFixup;
|
|
|
|
if (fixup == nsnull)
|
|
|
|
{
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
return fixup->QueryInterface(NS_GET_IID(nsIURIFixup), (void **) aURIFixup);
|
|
|
|
}
|
|
|
|
|