2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
*
|
2012-05-21 04:12:37 -07:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsReadableUtils.h"
|
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsEscape.h"
|
|
|
|
#include "nsCRT.h"
|
|
|
|
|
2008-05-09 17:53:37 -07:00
|
|
|
#include "nsIPlatformCharset.h"
|
2012-06-05 19:08:30 -07:00
|
|
|
#include "nsIFile.h"
|
2010-09-18 05:42:07 -07:00
|
|
|
|
|
|
|
#ifdef MOZ_TOOLKIT_SEARCH
|
2010-08-14 09:15:21 -07:00
|
|
|
#include "nsIBrowserSearchService.h"
|
2010-09-18 05:42:07 -07:00
|
|
|
#endif
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include "nsIURIFixup.h"
|
|
|
|
#include "nsDefaultURIFixup.h"
|
2011-06-24 18:55:46 -07:00
|
|
|
#include "mozilla/Preferences.h"
|
|
|
|
|
|
|
|
using namespace mozilla;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/* Implementation file */
|
|
|
|
NS_IMPL_ISUPPORTS1(nsDefaultURIFixup, nsIURIFixup)
|
|
|
|
|
|
|
|
nsDefaultURIFixup::nsDefaultURIFixup()
|
|
|
|
{
|
|
|
|
/* member initializers and constructor code */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsDefaultURIFixup::~nsDefaultURIFixup()
|
|
|
|
{
|
|
|
|
/* destructor code */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* nsIURI createExposableURI (in nsIRUI aURI); */
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDefaultURIFixup::CreateExposableURI(nsIURI *aURI, nsIURI **aReturn)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aURI);
|
|
|
|
NS_ENSURE_ARG_POINTER(aReturn);
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool isWyciwyg = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
aURI->SchemeIs("wyciwyg", &isWyciwyg);
|
|
|
|
|
|
|
|
nsCAutoString userPass;
|
|
|
|
aURI->GetUserPass(userPass);
|
|
|
|
|
|
|
|
// most of the time we can just AddRef and return
|
|
|
|
if (!isWyciwyg && userPass.IsEmpty())
|
|
|
|
{
|
|
|
|
*aReturn = aURI;
|
|
|
|
NS_ADDREF(*aReturn);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rats, we have to massage the URI
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
if (isWyciwyg)
|
|
|
|
{
|
|
|
|
nsCAutoString path;
|
|
|
|
nsresult rv = aURI->GetPath(path);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
PRUint32 pathLength = path.Length();
|
|
|
|
if (pathLength <= 2)
|
|
|
|
{
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Path is of the form "//123/http://foo/bar", with a variable number of digits.
|
|
|
|
// To figure out where the "real" URL starts, search path for a '/', starting at
|
|
|
|
// the third character.
|
|
|
|
PRInt32 slashIndex = path.FindChar('/', 2);
|
|
|
|
if (slashIndex == kNotFound)
|
|
|
|
{
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the charset of the original URI so we can pass it to our fixed up URI.
|
|
|
|
nsCAutoString charset;
|
|
|
|
aURI->GetOriginCharset(charset);
|
|
|
|
|
|
|
|
rv = NS_NewURI(getter_AddRefs(uri),
|
|
|
|
Substring(path, slashIndex + 1, pathLength - slashIndex - 1),
|
|
|
|
charset.get());
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// clone the URI so zapping user:pass doesn't change the original
|
|
|
|
nsresult rv = aURI->Clone(getter_AddRefs(uri));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
// hide user:pass unless overridden by pref
|
2011-09-28 23:19:26 -07:00
|
|
|
if (Preferences::GetBool("browser.fixup.hide_user_pass", true))
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
uri->SetUserPass(EmptyCString());
|
2011-06-24 18:55:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// return the fixed-up URI
|
|
|
|
*aReturn = uri;
|
|
|
|
NS_ADDREF(*aReturn);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* nsIURI createFixupURI (in nsAUTF8String aURIText, in unsigned long aFixupFlags); */
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDefaultURIFixup::CreateFixupURI(const nsACString& aStringURI, PRUint32 aFixupFlags, nsIURI **aURI)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(!aStringURI.IsEmpty());
|
|
|
|
NS_ENSURE_ARG_POINTER(aURI);
|
|
|
|
|
|
|
|
nsresult rv;
|
2012-07-30 07:20:58 -07:00
|
|
|
*aURI = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsCAutoString 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");
|
|
|
|
|
|
|
|
NS_ENSURE_TRUE(!uriString.IsEmpty(), NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIIOService> ioService = do_GetService(NS_IOSERVICE_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
nsCAutoString scheme;
|
|
|
|
ioService->ExtractScheme(aStringURI, scheme);
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
if (scheme.LowerCaseEqualsLiteral("view-source"))
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
PRUint32 newFixupFlags = aFixupFlags & ~FIXUP_FLAG_ALLOW_KEYWORD_LOOKUP;
|
|
|
|
|
|
|
|
rv = CreateFixupURI(Substring(uriString,
|
|
|
|
sizeof("view-source:") - 1,
|
|
|
|
uriString.Length() -
|
|
|
|
(sizeof("view-source:") - 1)),
|
|
|
|
newFixupFlags, getter_AddRefs(uri));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
nsCAutoString spec;
|
|
|
|
uri->GetSpec(spec);
|
|
|
|
uriString.Assign(NS_LITERAL_CSTRING("view-source:") + spec);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Check for if it is a file URL
|
|
|
|
FileURIFixup(uriString, aURI);
|
|
|
|
if(*aURI)
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
#if defined(XP_WIN) || defined(XP_OS2)
|
|
|
|
// Not a file URL, so translate '\' to '/' for convenience in the common protocols
|
|
|
|
// e.g. catch
|
|
|
|
//
|
|
|
|
// http:\\broken.com\address
|
|
|
|
// http:\\broken.com/blah
|
|
|
|
// broken.com\blah
|
|
|
|
//
|
|
|
|
// Code will also do partial fix up the following urls
|
|
|
|
//
|
|
|
|
// http:\\broken.com\address/somewhere\image.jpg (stops at first forward slash)
|
|
|
|
// http:\\broken.com\blah?arg=somearg\foo.jpg (stops at question mark)
|
|
|
|
// http:\\broken.com#odd\ref (stops at hash)
|
|
|
|
//
|
|
|
|
if (scheme.IsEmpty() ||
|
|
|
|
scheme.LowerCaseEqualsLiteral("http") ||
|
|
|
|
scheme.LowerCaseEqualsLiteral("https") ||
|
|
|
|
scheme.LowerCaseEqualsLiteral("ftp"))
|
|
|
|
{
|
|
|
|
// Walk the string replacing backslashes with forward slashes until
|
|
|
|
// the end is reached, or a question mark, or a hash, or a forward
|
|
|
|
// slash. The forward slash test is to stop before trampling over
|
|
|
|
// URIs which legitimately contain a mix of both forward and
|
|
|
|
// backward slashes.
|
|
|
|
nsCAutoString::iterator start;
|
|
|
|
nsCAutoString::iterator end;
|
|
|
|
uriString.BeginWriting(start);
|
|
|
|
uriString.EndWriting(end);
|
|
|
|
while (start != end) {
|
|
|
|
if (*start == '?' || *start == '#' || *start == '/')
|
|
|
|
break;
|
|
|
|
if (*start == '\\')
|
|
|
|
*start = '/';
|
|
|
|
++start;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2008-05-09 17:53:37 -07:00
|
|
|
// For these protocols, use system charset instead of the default UTF-8,
|
|
|
|
// if the URI is non ASCII.
|
2011-09-28 23:19:26 -07:00
|
|
|
bool bAsciiURI = IsASCII(uriString);
|
2011-11-09 07:02:14 -08:00
|
|
|
bool useUTF8 = (aFixupFlags & FIXUP_FLAG_USE_UTF8) ||
|
|
|
|
Preferences::GetBool("browser.fixup.use-utf8", false);
|
2011-09-28 23:19:26 -07:00
|
|
|
bool bUseNonDefaultCharsetForURI =
|
2011-11-09 07:02:14 -08:00
|
|
|
!bAsciiURI && !useUTF8 &&
|
2008-05-09 17:53:37 -07:00
|
|
|
(scheme.IsEmpty() ||
|
|
|
|
scheme.LowerCaseEqualsLiteral("http") ||
|
|
|
|
scheme.LowerCaseEqualsLiteral("https") ||
|
|
|
|
scheme.LowerCaseEqualsLiteral("ftp") ||
|
|
|
|
scheme.LowerCaseEqualsLiteral("file"));
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// Now we need to check whether "scheme" is something we don't
|
|
|
|
// really know about.
|
|
|
|
nsCOMPtr<nsIProtocolHandler> ourHandler, extHandler;
|
|
|
|
|
|
|
|
ioService->GetProtocolHandler(scheme.get(), getter_AddRefs(ourHandler));
|
|
|
|
extHandler = do_GetService(NS_NETWORK_PROTOCOL_CONTRACTID_PREFIX"default");
|
|
|
|
|
|
|
|
if (ourHandler != extHandler || !PossiblyHostPortUrl(uriString)) {
|
|
|
|
// Just try to create an URL out of it
|
2008-05-09 17:53:37 -07:00
|
|
|
rv = NS_NewURI(aURI, uriString,
|
2012-07-30 07:20:58 -07:00
|
|
|
bUseNonDefaultCharsetForURI ? GetCharsetForUrlBar() : nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (!*aURI && rv != NS_ERROR_MALFORMED_URI) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*aURI) {
|
|
|
|
if (aFixupFlags & FIXUP_FLAGS_MAKE_ALTERNATE_URI)
|
|
|
|
MakeAlternateURI(*aURI);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// See if it is a keyword
|
|
|
|
// Test whether keywords need to be fixed up
|
2011-09-28 23:19:26 -07:00
|
|
|
bool fixupKeywords = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (aFixupFlags & FIXUP_FLAG_ALLOW_KEYWORD_LOOKUP) {
|
2011-06-24 18:55:46 -07:00
|
|
|
nsresult rv = Preferences::GetBool("keyword.enabled", &fixupKeywords);
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_FAILURE);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (fixupKeywords)
|
|
|
|
{
|
|
|
|
KeywordURIFixup(uriString, aURI);
|
|
|
|
if(*aURI)
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prune duff protocol schemes
|
|
|
|
//
|
|
|
|
// ://totallybroken.url.com
|
|
|
|
// //shorthand.url.com
|
|
|
|
//
|
|
|
|
if (StringBeginsWith(uriString, NS_LITERAL_CSTRING("://")))
|
|
|
|
{
|
|
|
|
uriString = StringTail(uriString, uriString.Length() - 3);
|
|
|
|
}
|
|
|
|
else if (StringBeginsWith(uriString, NS_LITERAL_CSTRING("//")))
|
|
|
|
{
|
|
|
|
uriString = StringTail(uriString, uriString.Length() - 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add ftp:// or http:// to front of url if it has no spec
|
|
|
|
//
|
|
|
|
// Should fix:
|
|
|
|
//
|
|
|
|
// no-scheme.com
|
|
|
|
// ftp.no-scheme.com
|
|
|
|
// ftp4.no-scheme.com
|
|
|
|
// no-scheme.com/query?foo=http://www.foo.com
|
|
|
|
//
|
|
|
|
PRInt32 schemeDelim = uriString.Find("://",0);
|
|
|
|
PRInt32 firstDelim = uriString.FindCharInSet("/:");
|
|
|
|
if (schemeDelim <= 0 ||
|
|
|
|
(firstDelim != -1 && schemeDelim > firstDelim)) {
|
|
|
|
// find host name
|
|
|
|
PRInt32 hostPos = uriString.FindCharInSet("/:?#");
|
|
|
|
if (hostPos == -1)
|
|
|
|
hostPos = uriString.Length();
|
|
|
|
|
|
|
|
// extract host name
|
|
|
|
nsCAutoString hostSpec;
|
|
|
|
uriString.Left(hostSpec, hostPos);
|
|
|
|
|
|
|
|
// insert url spec corresponding to host name
|
|
|
|
if (IsLikelyFTP(hostSpec))
|
|
|
|
uriString.Assign(NS_LITERAL_CSTRING("ftp://") + uriString);
|
|
|
|
else
|
|
|
|
uriString.Assign(NS_LITERAL_CSTRING("http://") + uriString);
|
2008-05-09 17:53:37 -07:00
|
|
|
|
|
|
|
// For ftp & http, we want to use system charset.
|
2011-11-09 07:02:14 -08:00
|
|
|
if (!bAsciiURI && !useUTF8)
|
2011-10-17 07:59:28 -07:00
|
|
|
bUseNonDefaultCharsetForURI = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
} // end if checkprotocol
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
rv = NS_NewURI(aURI, uriString, bUseNonDefaultCharsetForURI ? GetCharsetForUrlBar() : nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// Did the caller want us to try an alternative URI?
|
|
|
|
// If so, attempt to fixup http://foo into http://www.foo.com
|
|
|
|
|
|
|
|
if (*aURI && aFixupFlags & FIXUP_FLAGS_MAKE_ALTERNATE_URI) {
|
|
|
|
MakeAlternateURI(*aURI);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we still haven't been able to construct a valid URI, try to force a
|
|
|
|
// keyword match. This catches search strings with '.' or ':' in them.
|
|
|
|
if (!*aURI && fixupKeywords)
|
|
|
|
{
|
|
|
|
KeywordToURI(aStringURI, aURI);
|
|
|
|
if(*aURI)
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsDefaultURIFixup::KeywordToURI(const nsACString& aKeyword,
|
|
|
|
nsIURI **aURI)
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
*aURI = nullptr;
|
2011-06-24 18:55:46 -07:00
|
|
|
NS_ENSURE_STATE(Preferences::GetRootBranch());
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-10-31 13:01:28 -07:00
|
|
|
// Strip leading "?" and leading/trailing spaces from aKeyword
|
|
|
|
nsCAutoString keyword(aKeyword);
|
|
|
|
if (StringBeginsWith(keyword, NS_LITERAL_CSTRING("?"))) {
|
|
|
|
keyword.Cut(0, 1);
|
|
|
|
}
|
|
|
|
keyword.Trim(" ");
|
|
|
|
|
2011-06-24 18:55:46 -07:00
|
|
|
nsAdoptingCString url = Preferences::GetLocalizedCString("keyword.URL");
|
|
|
|
if (!url) {
|
2007-03-22 10:30:00 -07:00
|
|
|
// Fall back to a non-localized pref, for backwards compat
|
2011-06-24 18:55:46 -07:00
|
|
|
url = Preferences::GetCString("keyword.URL");
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-08-14 09:15:21 -07:00
|
|
|
// If the pref is set and non-empty, use it.
|
|
|
|
if (!url.IsEmpty()) {
|
2010-10-31 13:01:28 -07:00
|
|
|
// Escape keyword, then prepend URL
|
2010-08-14 09:15:21 -07:00
|
|
|
nsCAutoString spec;
|
2010-10-31 13:01:28 -07:00
|
|
|
if (!NS_Escape(keyword, spec, url_XPAlphas)) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
spec.Insert(url, 0);
|
2010-08-14 09:15:21 -07:00
|
|
|
|
2012-03-23 16:27:54 -07:00
|
|
|
return NS_NewURI(aURI, spec);
|
2010-08-14 09:15:21 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-09-18 05:42:07 -07:00
|
|
|
#ifdef MOZ_TOOLKIT_SEARCH
|
2010-08-14 09:15:21 -07:00
|
|
|
// Try falling back to the search service's default search engine
|
|
|
|
nsCOMPtr<nsIBrowserSearchService> searchSvc = do_GetService("@mozilla.org/browser/search-service;1");
|
|
|
|
if (searchSvc) {
|
|
|
|
nsCOMPtr<nsISearchEngine> defaultEngine;
|
2010-08-27 13:39:20 -07:00
|
|
|
searchSvc->GetOriginalDefaultEngine(getter_AddRefs(defaultEngine));
|
2010-08-14 09:15:21 -07:00
|
|
|
if (defaultEngine) {
|
|
|
|
nsCOMPtr<nsISearchSubmission> submission;
|
2010-08-16 10:47:39 -07:00
|
|
|
// We want to allow default search plugins to specify alternate
|
|
|
|
// parameters that are specific to keyword searches. For the moment,
|
|
|
|
// do this by first looking for a magic
|
|
|
|
// "application/x-moz-keywordsearch" submission type. In the future,
|
|
|
|
// we should instead use a solution that relies on bug 587780.
|
2010-10-31 13:01:28 -07:00
|
|
|
defaultEngine->GetSubmission(NS_ConvertUTF8toUTF16(keyword),
|
2010-08-16 10:47:39 -07:00
|
|
|
NS_LITERAL_STRING("application/x-moz-keywordsearch"),
|
2010-08-14 09:15:21 -07:00
|
|
|
getter_AddRefs(submission));
|
2010-08-16 10:47:39 -07:00
|
|
|
// If getting the special x-moz-keywordsearch submission type failed,
|
|
|
|
// fall back to the default response type.
|
|
|
|
if (!submission) {
|
2010-10-31 13:01:28 -07:00
|
|
|
defaultEngine->GetSubmission(NS_ConvertUTF8toUTF16(keyword),
|
2010-08-16 10:47:39 -07:00
|
|
|
EmptyString(),
|
|
|
|
getter_AddRefs(submission));
|
|
|
|
}
|
|
|
|
|
2010-08-14 09:15:21 -07:00
|
|
|
if (submission) {
|
|
|
|
// The submission depends on POST data (i.e. the search engine's
|
|
|
|
// "method" is POST), we can't use this engine for keyword
|
|
|
|
// searches
|
|
|
|
nsCOMPtr<nsIInputStream> postData;
|
|
|
|
submission->GetPostData(getter_AddRefs(postData));
|
|
|
|
if (postData) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return submission->GetUri(aURI);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-09-18 05:42:07 -07:00
|
|
|
#endif
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-08-14 09:15:21 -07:00
|
|
|
// out of options
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool nsDefaultURIFixup::MakeAlternateURI(nsIURI *aURI)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-06-24 18:55:46 -07:00
|
|
|
if (!Preferences::GetRootBranch())
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2011-09-28 23:19:26 -07:00
|
|
|
if (!Preferences::GetBool("browser.fixup.alternate.enabled", true))
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Code only works for http. Not for any other protocol including https!
|
2011-09-28 23:19:26 -07:00
|
|
|
bool isHttp = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
aURI->SchemeIs("http", &isHttp);
|
|
|
|
if (!isHttp) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Security - URLs with user / password info should NOT be fixed up
|
|
|
|
nsCAutoString userpass;
|
|
|
|
aURI->GetUserPass(userpass);
|
|
|
|
if (!userpass.IsEmpty()) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 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.");
|
2011-06-24 18:55:46 -07:00
|
|
|
nsAdoptingCString prefPrefix =
|
|
|
|
Preferences::GetCString("browser.fixup.alternate.prefix");
|
|
|
|
if (prefPrefix)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
prefix.Assign(prefPrefix);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCAutoString suffix(".com");
|
2011-06-24 18:55:46 -07:00
|
|
|
nsAdoptingCString prefSuffix =
|
|
|
|
Preferences::GetCString("browser.fixup.alternate.suffix");
|
|
|
|
if (prefSuffix)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
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
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Do nothing
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (newHost.IsEmpty()) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Assign the new host string over the old one
|
|
|
|
aURI->SetHost(newHost);
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the host name starts with ftp\d*\. and it's not directly followed
|
|
|
|
* by the tld.
|
|
|
|
*/
|
2011-09-28 23:19:26 -07:00
|
|
|
bool nsDefaultURIFixup::IsLikelyFTP(const nsCString &aHostSpec)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-09-28 23:19:26 -07:00
|
|
|
bool likelyFTP = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (aHostSpec.EqualsIgnoreCase("ftp", 3)) {
|
|
|
|
nsACString::const_iterator iter;
|
|
|
|
nsACString::const_iterator end;
|
|
|
|
aHostSpec.BeginReading(iter);
|
|
|
|
aHostSpec.EndReading(end);
|
|
|
|
iter.advance(3); // move past the "ftp" part
|
|
|
|
|
|
|
|
while (iter != end)
|
|
|
|
{
|
|
|
|
if (*iter == '.') {
|
|
|
|
// now make sure the name has at least one more dot in it
|
|
|
|
++iter;
|
|
|
|
while (iter != end)
|
|
|
|
{
|
|
|
|
if (*iter == '.') {
|
2011-10-17 07:59:28 -07:00
|
|
|
likelyFTP = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
++iter;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (!nsCRT::IsAsciiDigit(*iter)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
++iter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return likelyFTP;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsDefaultURIFixup::FileURIFixup(const nsACString& aStringURI,
|
|
|
|
nsIURI** aURI)
|
|
|
|
{
|
|
|
|
nsCAutoString uriSpecOut;
|
|
|
|
|
|
|
|
nsresult rv = ConvertFileToStringURI(aStringURI, uriSpecOut);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
{
|
|
|
|
// if this is file url, uriSpecOut is already in FS charset
|
2012-07-30 07:20:58 -07:00
|
|
|
if(NS_SUCCEEDED(NS_NewURI(aURI, uriSpecOut.get(), nullptr)))
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsDefaultURIFixup::ConvertFileToStringURI(const nsACString& aIn,
|
|
|
|
nsCString& aOut)
|
|
|
|
{
|
2011-09-28 23:19:26 -07:00
|
|
|
bool attemptFixup = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#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() == '|')))
|
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
attemptFixup = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
Bug 627277 - Remove (broken) BeOS support. r=biesi,dwitte,gavin,joe,jorendorff,josh,khuey,mfinkle,neil,Pike,roc,shaver,smontagu,taras
2011-02-19 11:10:24 -08:00
|
|
|
#elif defined(XP_UNIX)
|
2007-03-22 10:30:00 -07:00
|
|
|
// Check if it starts with / (UNIX)
|
|
|
|
if(aIn.First() == '/')
|
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
attemptFixup = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
#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.
|
|
|
|
|
2012-06-05 19:08:30 -07:00
|
|
|
nsCOMPtr<nsIFile> filePath;
|
2007-03-22 10:30:00 -07:00
|
|
|
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_ConvertUTF8toUTF16 in(aIn);
|
|
|
|
if (PossiblyByteExpandedFileName(in)) {
|
|
|
|
// removes high byte
|
2011-10-17 07:59:28 -07:00
|
|
|
rv = NS_NewNativeLocalFile(NS_LossyConvertUTF16toASCII(in), false, getter_AddRefs(filePath));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// input is unicode
|
2011-10-17 07:59:28 -07:00
|
|
|
rv = NS_NewLocalFile(in, false, getter_AddRefs(filePath));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
{
|
|
|
|
NS_GetURLSpecFromFile(filePath, aOut);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool nsDefaultURIFixup::PossiblyHostPortUrl(const nsACString &aUrl)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// 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)
|
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
if (*iter == ':')
|
|
|
|
{
|
|
|
|
// Go onto checking the for the digits
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (*iter != '.')
|
|
|
|
{
|
|
|
|
// Whatever it is, it ain't a hostname!
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
++iter;
|
|
|
|
}
|
|
|
|
if (iter == iterEnd)
|
|
|
|
{
|
|
|
|
// No point continuing since there is no colon
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
++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!
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
++iter;
|
|
|
|
}
|
|
|
|
if (digitCount == 0 || digitCount > 5)
|
|
|
|
{
|
|
|
|
// No digits or more digits than a port would have.
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Yes, it's possibly a host:port url
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool nsDefaultURIFixup::PossiblyByteExpandedFileName(const nsAString& aIn)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// 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)
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
++iter;
|
|
|
|
}
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-05-09 17:53:37 -07:00
|
|
|
const char * nsDefaultURIFixup::GetFileSystemCharset()
|
|
|
|
{
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
const char * nsDefaultURIFixup::GetCharsetForUrlBar()
|
|
|
|
{
|
|
|
|
const char *charset = GetFileSystemCharset();
|
|
|
|
return charset;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult nsDefaultURIFixup::KeywordURIFixup(const nsACString & aURIString,
|
|
|
|
nsIURI** aURI)
|
|
|
|
{
|
|
|
|
// 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"
|
2010-03-08 16:30:02 -08:00
|
|
|
// Things that have a quote before the first dot/colon
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// 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"
|
|
|
|
|
2010-03-08 16:30:02 -08:00
|
|
|
// Note: PRUint32(kNotFound) is greater than any actual location
|
|
|
|
// in practice. So if we cast all locations to PRUint32, then a <
|
|
|
|
// b guarantees that either b is kNotFound and a is found, or both
|
|
|
|
// are found and a found before b.
|
|
|
|
PRUint32 dotLoc = PRUint32(aURIString.FindChar('.'));
|
|
|
|
PRUint32 colonLoc = PRUint32(aURIString.FindChar(':'));
|
|
|
|
PRUint32 spaceLoc = PRUint32(aURIString.FindChar(' '));
|
|
|
|
if (spaceLoc == 0) {
|
|
|
|
// Treat this as not found
|
|
|
|
spaceLoc = PRUint32(kNotFound);
|
|
|
|
}
|
|
|
|
PRUint32 qMarkLoc = PRUint32(aURIString.FindChar('?'));
|
|
|
|
PRUint32 quoteLoc = NS_MIN(PRUint32(aURIString.FindChar('"')),
|
|
|
|
PRUint32(aURIString.FindChar('\'')));
|
|
|
|
|
|
|
|
if (((spaceLoc < dotLoc || quoteLoc < dotLoc) &&
|
|
|
|
(spaceLoc < colonLoc || quoteLoc < colonLoc) &&
|
|
|
|
(spaceLoc < qMarkLoc || quoteLoc < qMarkLoc)) ||
|
2007-03-22 10:30:00 -07:00
|
|
|
qMarkLoc == 0)
|
|
|
|
{
|
|
|
|
KeywordToURI(aURIString, aURI);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(*aURI)
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult NS_NewURIFixup(nsIURIFixup **aURIFixup)
|
|
|
|
{
|
|
|
|
nsDefaultURIFixup *fixup = new nsDefaultURIFixup;
|
2012-07-30 07:20:58 -07:00
|
|
|
if (fixup == nullptr)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
return fixup->QueryInterface(NS_GET_IID(nsIURIFixup), (void **) aURIFixup);
|
|
|
|
}
|
|
|
|
|