From f4ac8b67b2bfeaa533d8617d114a32c0c2ceffd4 Mon Sep 17 00:00:00 2001 From: "darin%netscape.com" Date: Thu, 15 Aug 2002 18:38:46 +0000 Subject: [PATCH] second attempt at landing fix for bug 157131 "nsIIOService mods in preparation for freezing" r=dougt sr=alecf git-svn-id: svn://10.0.0.236/trunk@127379 18797224-902f-48f8-a5cc-f745e15eee43 --- .../base/src/nsContentAreaDragDrop.cpp | 9 +- .../ui/composer/content/ComposerCommands.js | 9 +- .../ui/composer/content/editorUtilities.js | 93 ++++++----- mozilla/extensions/cookie/nsCookies.cpp | 25 +-- mozilla/extensions/cookie/nsIImgManager.idl | 4 +- .../cookie/nsIPermissionManager.idl | 6 +- mozilla/extensions/cookie/nsImages.cpp | 17 +- mozilla/extensions/cookie/nsImages.h | 4 +- mozilla/extensions/cookie/nsImgManager.cpp | 4 +- .../extensions/cookie/nsPermissionManager.cpp | 21 ++- mozilla/extensions/cookie/nsPermissions.cpp | 82 ++++----- mozilla/extensions/cookie/nsPermissions.h | 11 +- mozilla/extensions/cookie/nsUtils.cpp | 4 +- mozilla/extensions/cookie/nsUtils.h | 2 +- .../wallet/signonviewer/SignonViewer.js | 2 +- .../extensions/wallet/src/nsWalletService.cpp | 6 +- mozilla/extensions/wallet/src/singsign.cpp | 55 ++---- mozilla/extensions/wallet/src/singsign.h | 6 +- mozilla/extensions/wallet/src/wallet.cpp | 4 +- .../mailnews/base/src/nsMessengerMigrator.cpp | 13 +- .../mailnews/compose/src/nsMsgAttachment.cpp | 34 ++-- mozilla/modules/libjar/nsJARURI.cpp | 30 ++-- .../plugin/base/src/nsPluginHostImpl.cpp | 24 ++- mozilla/netwerk/base/public/nsIIOService.idl | 157 +++++++----------- mozilla/netwerk/base/public/nsNetUtil.h | 6 +- mozilla/netwerk/base/src/nsIOService.cpp | 76 +-------- mozilla/netwerk/base/src/nsIOService.h | 2 + mozilla/netwerk/base/src/nsIOServiceMac.cpp | 12 +- mozilla/netwerk/base/src/nsIOServiceOS2.cpp | 11 +- mozilla/netwerk/base/src/nsIOServiceUnix.cpp | 11 +- mozilla/netwerk/base/src/nsIOServiceWin.cpp | 11 +- mozilla/netwerk/base/src/nsStandardURL.cpp | 35 ++-- mozilla/netwerk/base/src/nsStandardURL.h | 2 + mozilla/netwerk/base/src/nsURLHelper.cpp | 70 ++++++++ mozilla/netwerk/base/src/nsURLHelper.h | 17 ++ mozilla/netwerk/protocol/jar/src/Makefile.in | 3 + mozilla/netwerk/protocol/jar/src/nsJARURI.cpp | 30 ++-- .../protocol/res/src/nsResProtocolHandler.cpp | 9 +- .../protocol/res/src/nsResProtocolHandler.h | 2 + mozilla/rdf/base/src/nsRDFXMLDataSource.cpp | 5 +- .../datasource/src/nsFileSystemDataSource.cpp | 4 +- .../history/src/nsGlobalHistory.cpp | 7 +- .../urlbarhistory/src/nsUrlbarHistory.cpp | 21 ++- 43 files changed, 470 insertions(+), 486 deletions(-) diff --git a/mozilla/content/base/src/nsContentAreaDragDrop.cpp b/mozilla/content/base/src/nsContentAreaDragDrop.cpp index 1c3e4f02685..e0621e4be14 100644 --- a/mozilla/content/base/src/nsContentAreaDragDrop.cpp +++ b/mozilla/content/base/src/nsContentAreaDragDrop.cpp @@ -73,7 +73,7 @@ #include "nsISupportsPrimitives.h" #include "nsIServiceManagerUtils.h" #include "nsPromiseFlatString.h" -#include "nsIIOService.h" +#include "nsNetUtil.h" #include "nsIFile.h" #include "nsIWebNavigation.h" #include "nsIDragDropOverride.h" @@ -313,13 +313,12 @@ nsContentAreaDragDrop::ExtractURLFromData(const nsACString & inFlavor, nsISuppor } } else if ( inFlavor.Equals(kFileMime) ) { - // the data is a file. Use the IOService to get a file:// url + // the data is a file. Use the necko parsing utils to get a file:// url // from the OS data. - nsCOMPtr ioService(do_GetService("@mozilla.org/network/io-service;1")); nsCOMPtr file(do_QueryInterface(inDataWrapper)); - if ( ioService && file ) { + if ( file ) { nsCAutoString url; - ioService->GetURLSpecFromFile(file, url); + NS_GetURLSpecFromFile(file, url); outURL = NS_ConvertUTF8toUCS2(url); } } diff --git a/mozilla/editor/ui/composer/content/ComposerCommands.js b/mozilla/editor/ui/composer/content/ComposerCommands.js index 72afdcda20f..259b2a9a1c8 100644 --- a/mozilla/editor/ui/composer/content/ComposerCommands.js +++ b/mozilla/editor/ui/composer/content/ComposerCommands.js @@ -648,8 +648,7 @@ function PromptForSaveLocation(aDoSaveAsText, aEditorType, aMIMEType, ahtmlDocum var parentLocation = null; if (isLocalFile) { - var fileLocation = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsIFile); - ioService.initFileFromURLSpec(fileLocation, aDocumentURLString); // this asserts if url is not local + var fileLocation = ioService.getFileFromURLSpec(aDocumentURLString); // this asserts if url is not local parentLocation = fileLocation.parent; } if (parentLocation) @@ -1465,9 +1464,8 @@ function SaveDocument(aSaveAs, aSaveCopy, aMimeType) if (docURI.schemeIs("file")) { - tempLocalFile = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile); ioService = GetIOService(); - ioService.initFileFromURLSpec(tempLocalFile, urlstring); + tempLocalFile = ioService.getFileFromURLSpec(urlstring).QueryInterface(Components.interfaces.nsILocalFile); } } @@ -2102,9 +2100,8 @@ var nsValidateCommand = // See if it's a file: var ifile; try { - ifile = Components.classes["@mozilla.org/file/local;1"].createInstance().QueryInterface(Components.interfaces.nsIFile); var ioService = GetIOService(); - ioService.initFileFromURLSpec(ifile, URL2Validate); + ifile = ioService.getFileFromURLSpec(URL2Validate); // nsIFile throws an exception if it's not a file url } catch (e) { ifile = null; } if (ifile) diff --git a/mozilla/editor/ui/composer/content/editorUtilities.js b/mozilla/editor/ui/composer/content/editorUtilities.js index 61c21ee7096..f4242c9ca56 100644 --- a/mozilla/editor/ui/composer/content/editorUtilities.js +++ b/mozilla/editor/ui/composer/content/editorUtilities.js @@ -480,8 +480,8 @@ function MakeRelativeUrl(url) // Get just the file path part of the urls - var docPath = IOService.extractUrlPart(docUrl, IOService.url_Path); - var urlPath = IOService.extractUrlPart(inputUrl, IOService.url_Path); + var docPath = IOService.newURI(docUrl, null, null).path; + var urlPath = IOService.newURI(inputUrl, null, null).path; // We only return "urlPath", so we can convert // the entire docPath for case-insensitive comparisons @@ -648,9 +648,9 @@ function GetDocumentUrl() } // Extract the scheme (e.g., 'file', 'http') from a URL string -function GetScheme(url) +function GetScheme(urlspec) { - var resultUrl = TrimString(url); + var resultUrl = TrimString(urlspec); // Unsaved document URL has no acceptable scheme yet if (!resultUrl || IsUrlAboutBlank(resultUrl)) return ""; @@ -665,13 +665,13 @@ function GetScheme(url) var scheme = ""; try { // This fails if there's no scheme - scheme = IOService.extractScheme(resultUrl, {schemeStartPos:0}, {schemeEndPos:0}); + scheme = IOService.extractScheme(resultUrl); } catch (e) {} return scheme ? scheme.toLowerCase() : ""; } -function GetHost(url) +function GetHost(urlspec) { if (!url) return ""; @@ -682,13 +682,13 @@ function GetHost(url) var host = ""; try { - host = IOService.extractUrlPart(url, IOService.url_Host); + host = IOService.newURI(urlspec, null, null).host; } catch (e) {} return host; } -function GetUsername(url) +function GetUsername(urlspec) { if (!url) return ""; @@ -699,15 +699,15 @@ function GetUsername(url) var username = ""; try { - username = IOService.extractUrlPart(url, IOService.url_Username); + username = IOService.newURI(urlspec, null, null).username; } catch (e) {} return username; } -function GetFilename(url) +function GetFilename(urlspec) { - if (!url || IsUrlAboutBlank(url)) + if (!urlspec || IsUrlAboutBlank(urlspec)) return ""; var IOService = GetIOService(); @@ -717,12 +717,12 @@ function GetFilename(url) var filename; try { - filename = IOService.extractUrlPart(url, IOService.url_FileBaseName); - if (filename) + uri = IOService.newURI(urlspec, null, null); + if (uri) { - var ext = IOService.extractUrlPart(url, IOService.url_FileExtension); - if (ext) - filename += "."+ext; + var url = uri.QueryInterface(Components.interfaces.nsIURL); + if (url) + filename = url.fileName; } } catch (e) {} @@ -732,11 +732,11 @@ function GetFilename(url) // Return the url without username and password // Optional output objects return extracted username and password strings // This uses just string routines via nsIIOServices -function StripUsernamePassword(url, usernameObj, passwordObj) +function StripUsernamePassword(urlspec, usernameObj, passwordObj) { - url = TrimString(url); - if (!url || IsUrlAboutBlank(url)) - return url; + urlspec = TrimString(urlspec); + if (!urlspec || IsUrlAboutBlank(urlspec)) + return urlspec; if (usernameObj) usernameObj.value = ""; @@ -744,16 +744,17 @@ function StripUsernamePassword(url, usernameObj, passwordObj) passwordObj.value = ""; // "@" must exist else we will never detect username or password - var atIndex = url.indexOf("@"); + var atIndex = urlspec.indexOf("@"); if (atIndex > 0) { try { var IOService = GetIOService(); if (!IOService) - return url; + return urlspec; - var username = IOService.extractUrlPart(url, IOService.url_Username); - var password = IOService.extractUrlPart(url, IOService.url_Password); + var uri = IOService.newURI(urlspec, null, null); + var username = uri.username; + var password = uri.password; if (usernameObj && username) usernameObj.value = username; @@ -761,79 +762,79 @@ function StripUsernamePassword(url, usernameObj, passwordObj) passwordObj.value = password; if (username) { - var usernameStart = url.indexOf(username); + var usernameStart = urlspec.indexOf(username); if (usernameStart != -1) - return url.slice(0, usernameStart) + url.slice(atIndex+1); + return urlspec.slice(0, usernameStart) + urlspec.slice(atIndex+1); } } catch (e) {} } - return url; + return urlspec; } -function StripPassword(url, passwordObj) +function StripPassword(urlspec, passwordObj) { - url = TrimString(url); - if (!url || IsUrlAboutBlank(url)) - return url; + urlspec = TrimString(urlspec); + if (!urlspec || IsUrlAboutBlank(urlspec)) + return urlspec; if (passwordObj) passwordObj.value = ""; // "@" must exist else we will never detect password - var atIndex = url.indexOf("@"); + var atIndex = urlspec.indexOf("@"); if (atIndex > 0) { try { var IOService = GetIOService(); if (!IOService) - return url; + return urlspec; - var password = IOService.extractUrlPart(url, IOService.url_Password); + var password = IOService.newURI(urlspec, null, null).password; if (passwordObj && password) passwordObj.value = password; if (password) { // Find last ":" before "@" - var colon = url.lastIndexOf(":", atIndex); + var colon = urlspec.lastIndexOf(":", atIndex); if (colon != -1) { // Include the "@" - return url.slice(0, colon) + url.slice(atIndex); + return urlspec.slice(0, colon) + urlspec.slice(atIndex); } } } catch (e) {} } - return url; + return urlspec; } // Version to use when you have an nsIURI object function StripUsernamePasswordFromURI(uri) { - var url = ""; + var urlspec = ""; if (uri) { try { - url = uri.spec; + urlspec = uri.spec; var userPass = uri.userPass; if (userPass) { - start = url.indexOf(userPass); - url = url.slice(0, start) + url.slice(start+userPass.length+1); + start = urlspec.indexOf(userPass); + urlspec = urlspec.slice(0, start) + urlspec.slice(start+userPass.length+1); } } catch (e) {} } - return url; + return urlspec; } -function InsertUsernameIntoUrl(url, username) +function InsertUsernameIntoUrl(urlspec, username) { - if (!url || !username) - return url; + if (!urlspec || !username) + return urlspec; try { var ioService = GetIOService(); - var URI = ioService.newURI(url, window.editorShell.GetDocumentCharacterSet(), null); + var URI = ioService.newURI(urlspec, window.editorShell.GetDocumentCharacterSet(), null); URI.username = username; return URI.spec; } catch (e) {} diff --git a/mozilla/extensions/cookie/nsCookies.cpp b/mozilla/extensions/cookie/nsCookies.cpp index c50df43c331..36f6c42cbeb 100644 --- a/mozilla/extensions/cookie/nsCookies.cpp +++ b/mozilla/extensions/cookie/nsCookies.cpp @@ -1306,7 +1306,6 @@ cookie_SetCookieString(nsIURI * curURL, nsIPrompt *aPrompter, const char * setCo } /* generate the message for the nag box */ - PRUnichar * new_string=0; int count = cookie_Count(host_from_header); prev_cookie = cookie_CheckForPrevCookie (path_from_header, host_from_header, name_from_header); @@ -1495,9 +1494,8 @@ COOKIE_SetCookieStringFromHttp(nsIURI * curURL, nsIURI * firstURL, nsIPrompt *aP status = cookie_P3PDecision(curURL, firstURL, aHttpChannel); if (status == nsICookie::STATUS_REJECTED) { nsCOMPtr os(do_GetService("@mozilla.org/observer-service;1")); - if (os) { - nsresult rv = os->NotifyObservers(nsnull, "cookieIcon", NS_LITERAL_STRING("on").get()); - } + if (os) + os->NotifyObservers(nsnull, "cookieIcon", NS_LITERAL_STRING("on").get()); return; } } @@ -1666,7 +1664,7 @@ COOKIE_Read() { } cookie_CookieStruct *new_cookie, *tmp_cookie_ptr; size_t new_len; - nsAutoString buffer; + nsCAutoString buffer; PRBool added_to_list; nsFileSpec dirSpec; nsresult rv = CKutil_ProfileDirectory(dirSpec); @@ -1693,7 +1691,7 @@ COOKIE_Read() { added_to_list = PR_FALSE; if ( !buffer.IsEmpty() ) { - PRUnichar firstChar = buffer.CharAt(0); + char firstChar = buffer.CharAt(0); if (firstChar == '#' || firstChar == nsCRT::CR || firstChar == nsCRT::LF || firstChar == 0) { continue; @@ -1709,7 +1707,7 @@ COOKIE_Read() { (cookieIndex=buffer.FindChar('\t', nameIndex)+1) == 0 ) { continue; } - nsAutoString host, isDomain, path, isSecure, expires, name, cookie; + nsCAutoString host, isDomain, path, isSecure, expires, name, cookie; buffer.Mid(host, hostIndex, isDomainIndex-hostIndex-1); buffer.Mid(isDomain, isDomainIndex, pathIndex-isDomainIndex-1); buffer.Mid(path, pathIndex, secureIndex-pathIndex-1); @@ -1729,12 +1727,12 @@ COOKIE_Read() { new_cookie->cookie = ToNewCString(cookie); new_cookie->host = ToNewCString(host); new_cookie->path = ToNewCString(path); - if (isDomain.Equals(NS_LITERAL_STRING("TRUE"))) { + if (isDomain.Equals(NS_LITERAL_CSTRING("TRUE"))) { new_cookie->isDomain = PR_TRUE; } else { new_cookie->isDomain = PR_FALSE; } - if (isSecure.Equals(NS_LITERAL_STRING("TRUE"))) { + if (isSecure.Equals(NS_LITERAL_CSTRING("TRUE"))) { new_cookie->isSecure = PR_TRUE; } else { new_cookie->isSecure = PR_FALSE; @@ -1913,17 +1911,12 @@ COOKIE_Remove (PL_strcmp(cookie->name, name) == 0) && (PL_strcmp(cookie->path, path) == 0)) { if (blocked && cookie->host) { - char * hostname = nsnull; char * hostnameAfterDot = cookie->host; while (*hostnameAfterDot == '.') { hostnameAfterDot++; } - CKutil_StrAllocCopy(hostname, hostnameAfterDot); - if (hostname) { - if (NS_SUCCEEDED(PERMISSION_Read())) { - Permission_AddHost(hostname, PR_FALSE, COOKIEPERMISSION, PR_TRUE); - } - } + if (NS_SUCCEEDED(PERMISSION_Read())) + Permission_AddHost(nsDependentCString(hostnameAfterDot), PR_FALSE, COOKIEPERMISSION, PR_TRUE); } cookie_list->RemoveElementAt(count); deleteCookie((void*)cookie, nsnull); diff --git a/mozilla/extensions/cookie/nsIImgManager.idl b/mozilla/extensions/cookie/nsIImgManager.idl index 5c89972299e..0b716326ecf 100644 --- a/mozilla/extensions/cookie/nsIImgManager.idl +++ b/mozilla/extensions/cookie/nsIImgManager.idl @@ -44,10 +44,12 @@ #include "nsISupports.idl" +interface nsIURI; + [scriptable, uuid(D60B3710-166D-11d5-A542-0010A401EB10)] interface nsIImgManager : nsISupports { - void block(in string imageURL); + void block(in nsIURI imageURL); }; %{ C++ diff --git a/mozilla/extensions/cookie/nsIPermissionManager.idl b/mozilla/extensions/cookie/nsIPermissionManager.idl index 139077e7c1f..de0ecd660d2 100644 --- a/mozilla/extensions/cookie/nsIPermissionManager.idl +++ b/mozilla/extensions/cookie/nsIPermissionManager.idl @@ -48,11 +48,11 @@ [scriptable, uuid(4F6B5E00-0C36-11d5-A535-0010A401EB10)] interface nsIPermissionManager : nsISupports { - void add(in string objectURL, in boolean permission, in PRInt32 type); - boolean testForBlocking(in string objectURL, in PRInt32 type); + void add(in AUTF8String objectURL, in boolean permission, in PRInt32 type); + boolean testForBlocking(in AUTF8String objectURL, in PRInt32 type); void removeAll(); readonly attribute nsISimpleEnumerator enumerator; - void remove(in string host, in PRInt32 type); + void remove(in AUTF8String host, in PRInt32 type); }; %{ C++ diff --git a/mozilla/extensions/cookie/nsImages.cpp b/mozilla/extensions/cookie/nsImages.cpp index 8e20aa780c5..c01b208e3a7 100644 --- a/mozilla/extensions/cookie/nsImages.cpp +++ b/mozilla/extensions/cookie/nsImages.cpp @@ -45,6 +45,7 @@ #include "nsIPref.h" #include "nsIServiceManager.h" #include "nsIIOService.h" +#include "nsIURI.h" #define image_behaviorPref "network.image.imageBehavior" #define image_warningPref "network.image.warnAboutImages" @@ -210,17 +211,13 @@ IMAGE_BlockedInMail() } PUBLIC nsresult -IMAGE_Block(const char* imageURL, - nsIIOService* ioService) { - if (!imageURL || !(*imageURL)) { +IMAGE_Block(nsIURI* imageURI) +{ + if (!imageURI) { return NS_ERROR_NULL_POINTER; } - nsresult rv = NS_OK; - nsCAutoString host; - NS_ASSERTION(ioService, "IOService not available"); - rv = ioService->ExtractUrlPart(nsDependentCString(imageURL), - nsIIOService::url_Host | - nsIIOService::url_Port, host); - Permission_AddHost((char*)host.get(), PR_FALSE, IMAGEPERMISSION, PR_TRUE); + nsCAutoString hostPort; + imageURI->GetHostPort(hostPort); + Permission_AddHost(hostPort, PR_FALSE, IMAGEPERMISSION, PR_TRUE); return NS_OK; } diff --git a/mozilla/extensions/cookie/nsImages.h b/mozilla/extensions/cookie/nsImages.h index a6ecf84fb43..f5008084d72 100644 --- a/mozilla/extensions/cookie/nsImages.h +++ b/mozilla/extensions/cookie/nsImages.h @@ -41,10 +41,12 @@ #include "nsString.h" +class nsIURI; + extern nsresult IMAGE_CheckForPermission (const char * hostname, const char * firstHostname, PRBool *permission); extern PRBool IMAGE_BlockedInMail(void); -extern nsresult IMAGE_Block(const char * imageURL, nsIIOService *ioService); +extern nsresult IMAGE_Block(nsIURI * imageURI); extern void IMAGE_RegisterPrefCallbacks(void); #endif /* IMAGES_H */ diff --git a/mozilla/extensions/cookie/nsImgManager.cpp b/mozilla/extensions/cookie/nsImgManager.cpp index 856d052e059..17db610324f 100644 --- a/mozilla/extensions/cookie/nsImgManager.cpp +++ b/mozilla/extensions/cookie/nsImgManager.cpp @@ -92,9 +92,9 @@ nsresult nsImgManager::Init() } -NS_IMETHODIMP nsImgManager::Block(const char * imageURL) +NS_IMETHODIMP nsImgManager::Block(nsIURI* imageURI) { - ::IMAGE_Block(imageURL,mIOService); + ::IMAGE_Block(imageURI); return NS_OK; } diff --git a/mozilla/extensions/cookie/nsPermissionManager.cpp b/mozilla/extensions/cookie/nsPermissionManager.cpp index d7e548d6987..1a16abcdc17 100644 --- a/mozilla/extensions/cookie/nsPermissionManager.cpp +++ b/mozilla/extensions/cookie/nsPermissionManager.cpp @@ -47,7 +47,7 @@ #include "nsIPrompt.h" #include "nsIObserverService.h" #include "nsPermission.h" -#include "nsNetCID.h" +#include "nsNetUtil.h" static NS_DEFINE_CID(kIOServiceCID, NS_IOSERVICE_CID); @@ -129,19 +129,26 @@ nsresult nsPermissionManager::Init() observerService->AddObserver(this, "profile-before-change", PR_FALSE); observerService->AddObserver(this, "profile-do-change", PR_FALSE); } - mIOService = do_GetService(NS_IOSERVICE_CONTRACTID, &rv); + + mIOService = do_GetIOService(); return rv; } NS_IMETHODIMP nsPermissionManager::Add - (const char * objectURL, PRBool permission, PRInt32 type) { - ::PERMISSION_Add(objectURL, permission, type, mIOService); + (const nsACString & objectURI, PRBool permission, PRInt32 type) { + // XXX ideally we should change this interface to pass nsIURI + nsCOMPtr uri; + NS_NewURI(getter_AddRefs(uri), objectURI, nsnull, nsnull, mIOService); + ::PERMISSION_Add(uri, permission, type); return NS_OK; } NS_IMETHODIMP nsPermissionManager::TestForBlocking - (const char * objectURL, PRInt32 type, PRBool* blocked) { - ::PERMISSION_TestForBlocking(objectURL, blocked, type, mIOService); + (const nsACString &objectURI, PRInt32 type, PRBool* blocked) { + // XXX ideally we should change this interface to pass nsIURI + nsCOMPtr uri; + NS_NewURI(getter_AddRefs(uri), objectURI, nsnull, nsnull, mIOService); + ::PERMISSION_TestForBlocking(uri, blocked, type); return NS_OK; } @@ -162,7 +169,7 @@ NS_IMETHODIMP nsPermissionManager::GetEnumerator(nsISimpleEnumerator * *entries) return NS_OK; } -NS_IMETHODIMP nsPermissionManager::Remove(const char* host, PRInt32 type) { +NS_IMETHODIMP nsPermissionManager::Remove(const nsACString & host, PRInt32 type) { ::PERMISSION_Remove(host, type); return NS_OK; } diff --git a/mozilla/extensions/cookie/nsPermissions.cpp b/mozilla/extensions/cookie/nsPermissions.cpp index 47b48e0ba0e..0ecd5c1f74a 100644 --- a/mozilla/extensions/cookie/nsPermissions.cpp +++ b/mozilla/extensions/cookie/nsPermissions.cpp @@ -50,7 +50,7 @@ #include "nsVoidArray.h" #include "prmem.h" #include "nsAppDirectoryServiceDefs.h" -#include "nsIIOService.h" +#include "nsIURI.h" #include "nsNetCID.h" #include "nsTextFormatter.h" #include "nsIObserverService.h" @@ -213,14 +213,12 @@ Permission_Check( /* see if we need to remember this decision */ if (rememberChecked) { - char * hostname2 = NULL; /* ignore leading periods in host name */ const char * hostnameAfterDot = hostname; while (hostnameAfterDot && (*hostnameAfterDot == '.')) { hostnameAfterDot++; } - CKutil_StrAllocCopy(hostname2, hostnameAfterDot); - Permission_AddHost(hostname2, permission, type, PR_TRUE); + Permission_AddHost(nsDependentCString(hostnameAfterDot), permission, type, PR_TRUE); } if (rememberChecked != permission_GetRememberChecked(type)) { permission_SetRememberChecked(type, rememberChecked); @@ -232,14 +230,12 @@ Permission_Check( } PUBLIC nsresult -Permission_AddHost(char * host, PRBool permission, PRInt32 type, PRBool save) { +Permission_AddHost(const nsAFlatCString & host, PRBool permission, PRInt32 type, PRBool save) { /* create permission list if it does not yet exist */ if(!permission_list) { permission_list = new nsVoidArray(); - if(!permission_list) { - Recycle(host); + if(!permission_list) return NS_ERROR_OUT_OF_MEMORY; - } } /* find existing entry for host */ @@ -250,14 +246,13 @@ Permission_AddHost(char * host, PRBool permission, PRInt32 type, PRBool save) { for (i = 0; i < hostCount; ++i) { hostStruct = NS_STATIC_CAST(permission_HostStruct*, permission_list->ElementAt(i)); if (hostStruct) { - if (PL_strcasecmp(host,hostStruct->host)==0) { + if (PL_strcasecmp(host.get(),hostStruct->host)==0) { /* host found in list */ - Recycle(host); HostFound = PR_TRUE; break; #ifdef alphabetize - } else if (PL_strcasecmp(host, hostStruct->host) < 0) { + } else if (PL_strcasecmp(host.get(), hostStruct->host) < 0) { /* need to insert new entry here */ break; @@ -270,15 +265,12 @@ Permission_AddHost(char * host, PRBool permission, PRInt32 type, PRBool save) { /* create a host structure for the host */ hostStruct = PR_NEW(permission_HostStruct); - if (!hostStruct) { - Recycle(host); + if (!hostStruct) return NS_ERROR_FAILURE; - } - hostStruct->host = host; + hostStruct->host = ToNewCString(host); hostStruct->permissionList = new nsVoidArray(); if(!hostStruct->permissionList) { PR_Free(hostStruct); - Recycle(host); return NS_ERROR_FAILURE; } @@ -323,7 +315,7 @@ Permission_AddHost(char * host, PRBool permission, PRInt32 type, PRBool save) { } PRIVATE void -permission_Unblock(char * host, PRInt32 type) { +permission_Unblock(const char * host, PRInt32 type) { /* nothing to do if permission list does not exist */ if(!permission_list) { @@ -465,7 +457,7 @@ PERMISSION_Read() { if(!permission_list) { return NS_ERROR_OUT_OF_MEMORY; } - nsAutoString buffer; + nsCAutoString buffer; nsFileSpec dirSpec; nsresult rv = CKutil_ProfileDirectory(dirSpec); if (NS_FAILED(rv)) { @@ -486,7 +478,7 @@ PERMISSION_Read() { */ while(CKutil_GetLine(strm,buffer) != -1) { if ( !buffer.IsEmpty() ) { - PRUnichar firstChar = buffer.CharAt(0); + char firstChar = buffer.CharAt(0); if (firstChar == '#' || firstChar == nsCRT::CR || firstChar == nsCRT::LF || firstChar == 0) { continue; @@ -506,10 +498,9 @@ PERMISSION_Read() { hostIndex++; } - nsAutoString host; - buffer.Mid(host, hostIndex, permissionIndex-hostIndex-1); + nsDependentCSubstring host(buffer, hostIndex, permissionIndex-hostIndex-1); - nsAutoString permissionString; + nsCAutoString permissionString; for (;;) { if (nextPermissionIndex == buffer.Length()+1) { break; @@ -517,6 +508,8 @@ PERMISSION_Read() { if ((nextPermissionIndex=buffer.FindChar('\t', permissionIndex)+1) == 0) { nextPermissionIndex = buffer.Length()+1; } + // XXX would like to use nsDependentCSubstring to avoid this allocation, + // but it unfortunately doesn't provide the CharAt method. buffer.Mid(permissionString, permissionIndex, nextPermissionIndex-permissionIndex-1); permissionIndex = nextPermissionIndex; @@ -540,13 +533,13 @@ PERMISSION_Read() { * a host value of "@@@@" is a special code designating the * state of the nag-box's checkmark */ - if (host.Equals(NS_LITERAL_STRING("@@@@"))) { + if (host.Equals(NS_LITERAL_CSTRING("@@@@"))) { if (!permissionString.IsEmpty()) { permission_SetRememberChecked(type, permission); } } else { if (!permissionString.IsEmpty()) { - rv = Permission_AddHost(ToNewCString(host), permission, type, PR_FALSE); + rv = Permission_AddHost(PromiseFlatCString(host), permission, type, PR_FALSE); if (NS_FAILED(rv)) { strm.close(); return rv; @@ -640,7 +633,7 @@ permission_remove (PRInt32 hostNumber, PRInt32 type) { } PUBLIC void -PERMISSION_Remove(const char* host, PRInt32 type) { +PERMISSION_Remove(const nsACString & host, PRInt32 type) { /* get to the indicated host in the list */ if (permission_list) { @@ -651,7 +644,7 @@ PERMISSION_Remove(const char* host, PRInt32 type) { hostStruct = NS_STATIC_CAST(permission_HostStruct*, permission_list->ElementAt(hostCount)); NS_ASSERTION(hostStruct, "corrupt permission list"); - if ((PL_strcmp(hostStruct->host, host) == 0)) { + if (host.Equals(hostStruct->host)) { /* get to the indicated permission in the list */ PRInt32 typeCount = hostStruct->permissionList->Count(); @@ -709,17 +702,14 @@ PERMISSION_DeletePersistentUserData(void) } PUBLIC void -PERMISSION_Add(const char * objectURL, PRBool permission, PRInt32 type, - nsIIOService* ioService) { - if (!objectURL) { +PERMISSION_Add(nsIURI * objectURI, PRBool permission, PRInt32 type) { + if (!objectURI) { return; } - nsresult rv = NS_OK; - nsCAutoString host; - NS_ASSERTION(ioService, "IOService not available"); - rv = ioService->ExtractUrlPart(nsDependentCString(objectURL), - nsIIOService::url_Host | - nsIIOService::url_Port, host); + nsCAutoString hostPort; + objectURI->GetHostPort(hostPort); + if (hostPort.IsEmpty()) + return; /* * if permission is false, it will be added to the permission list @@ -727,34 +717,32 @@ PERMISSION_Add(const char * objectURL, PRBool permission, PRInt32 type, * true permission being added */ if (permission) { - char * hostPtr = (char *)host.get(); + const char * hostPtr = hostPort.get(); while (PR_TRUE) { permission_Unblock(hostPtr, type); hostPtr = PL_strchr(hostPtr, '.'); if (!hostPtr) { break; } - hostPtr++; /* get passed the period */ + hostPtr++; /* get past the period */ } return; } - Permission_AddHost(ToNewCString(host), permission, type, PR_TRUE); + Permission_AddHost(hostPort, permission, type, PR_TRUE); } PUBLIC void -PERMISSION_TestForBlocking(const char * objectURL, PRBool* blocked, PRInt32 type, - nsIIOService* ioService) { - if (!objectURL) { +PERMISSION_TestForBlocking(nsIURI * objectURI, PRBool* blocked, PRInt32 type) { + if (!objectURI) { return; } nsresult rv = NS_OK; - nsCAutoString host; - NS_ASSERTION(ioService, "IOService not available"); - rv = ioService->ExtractUrlPart(nsDependentCString(objectURL), - nsIIOService::url_Host | - nsIIOService::url_Port, host); + nsCAutoString hostPort; + objectURI->GetHostPort(hostPort); + if (hostPort.IsEmpty()) + return; - const char * hostPtr = host.get(); + const char * hostPtr = hostPort.get(); while (PR_TRUE) { PRBool permission; rv = permission_CheckFromList(hostPtr, permission, type); diff --git a/mozilla/extensions/cookie/nsPermissions.h b/mozilla/extensions/cookie/nsPermissions.h index 8bd2fdd911d..139699c783a 100644 --- a/mozilla/extensions/cookie/nsPermissions.h +++ b/mozilla/extensions/cookie/nsPermissions.h @@ -40,7 +40,8 @@ #define PERMISSIONS_H #include "nsString.h" -#include "nsIIOService.h" + +class nsIURI; #define COOKIEPERMISSION 0 #define IMAGEPERMISSION 1 @@ -56,8 +57,8 @@ typedef enum { class nsIPrompt; extern nsresult PERMISSION_Read(); -extern void PERMISSION_Add(const char * objectURL, PRBool permission, PRInt32 type, nsIIOService* ioService); -extern void PERMISSION_TestForBlocking(const char * objectURL, PRBool* blocked, PRInt32 type, nsIIOService* ioService); +extern void PERMISSION_Add(nsIURI * objectURI, PRBool permission, PRInt32 type); +extern void PERMISSION_TestForBlocking(nsIURI * objectURL, PRBool* blocked, PRInt32 type); extern void PERMISSION_RemoveAll(); extern void PERMISSION_DeletePersistentUserData(void); @@ -65,13 +66,13 @@ extern PRInt32 PERMISSION_HostCount(); extern PRInt32 PERMISSION_TypeCount(PRInt32 host); extern nsresult PERMISSION_Enumerate (PRInt32 hostNumber, PRInt32 typeNumber, char **host, PRInt32 *type, PRBool *capability); -extern void PERMISSION_Remove(const char* host, PRInt32 type); +extern void PERMISSION_Remove(const nsACString & host, PRInt32 type); extern PRBool Permission_Check (nsIPrompt *aPrompter, const char * hostname, PRInt32 type, PRBool warningPref, const char * message_string, int count_for_message); extern nsresult Permission_AddHost - (char * host, PRBool permission, PRInt32 type, PRBool save); + (const nsAFlatCString &host, PRBool permission, PRInt32 type, PRBool save); //extern void Permission_Free(PRInt32 hostNumber, PRInt32 type, PRBool save); extern void Permission_Save(PRBool notify); diff --git a/mozilla/extensions/cookie/nsUtils.cpp b/mozilla/extensions/cookie/nsUtils.cpp index 6eb1d71947a..3f4bb99780c 100644 --- a/mozilla/extensions/cookie/nsUtils.cpp +++ b/mozilla/extensions/cookie/nsUtils.cpp @@ -79,7 +79,7 @@ ckutil_getChar(nsInputFileStream& strm, char& c) { * strip carriage returns and line feeds from end of line */ PUBLIC PRInt32 -CKutil_GetLine(nsInputFileStream& strm, nsString& aLine) { +CKutil_GetLine(nsInputFileStream& strm, nsACString& aLine) { /* read the line */ aLine.Truncate(); @@ -93,7 +93,7 @@ CKutil_GetLine(nsInputFileStream& strm, nsString& aLine) { } if (c != '\r') { - aLine.Append(PRUnichar(c)); + aLine.Append(c); } } return 0; diff --git a/mozilla/extensions/cookie/nsUtils.h b/mozilla/extensions/cookie/nsUtils.h index 73030ba0ed2..8cc88d475ec 100644 --- a/mozilla/extensions/cookie/nsUtils.h +++ b/mozilla/extensions/cookie/nsUtils.h @@ -42,7 +42,7 @@ #include "nsString.h" #include "nsFileStream.h" -extern PRInt32 CKutil_GetLine(nsInputFileStream& strm, nsString& aLine); +extern PRInt32 CKutil_GetLine(nsInputFileStream& strm, nsACString& aLine); extern PRUnichar* CKutil_Localize(const PRUnichar *genericString); extern nsresult CKutil_ProfileDirectory(nsFileSpec& dirSpec); extern char * CKutil_StrAllocCopy(char *&destination, const char *source); diff --git a/mozilla/extensions/wallet/signonviewer/SignonViewer.js b/mozilla/extensions/wallet/signonviewer/SignonViewer.js index 623b8ea8ad0..2b12651c201 100644 --- a/mozilla/extensions/wallet/signonviewer/SignonViewer.js +++ b/mozilla/extensions/wallet/signonviewer/SignonViewer.js @@ -218,7 +218,7 @@ function LoadSignons() { .getService(Components.interfaces.nsIIOService); var username; try { - username = ioService.extractUrlPart(host, ioService.url_Username, unused, unused); + username = ioService.newURI(host, null, null).username; } catch(e) { username = ""; } diff --git a/mozilla/extensions/wallet/src/nsWalletService.cpp b/mozilla/extensions/wallet/src/nsWalletService.cpp index b901405c473..f22e220aba1 100644 --- a/mozilla/extensions/wallet/src/nsWalletService.cpp +++ b/mozilla/extensions/wallet/src/nsWalletService.cpp @@ -449,10 +449,6 @@ nsWalletlibService::OnStateChange(nsIWebProgress* aWebProgress, return NS_OK; } - nsCAutoString spec; - rv = uri->GetSpec(spec); - if (NS_FAILED(rv)) return rv; - nsCOMPtr forms; rv = htmldoc->GetForms(getter_AddRefs(forms)); if (NS_FAILED(rv) || (forms == nsnull)) return rv; @@ -529,7 +525,7 @@ nsWalletlibService::OnStateChange(nsIWebProgress* aWebProgress, wwatch->GetNewPrompter(0, getter_AddRefs(prompter)); } if (prompter) { - SINGSIGN_RestoreSignonData(prompter, spec.get(), nameString, &valueString, elementNumber++); + SINGSIGN_RestoreSignonData(prompter, uri, nameString, &valueString, elementNumber++); } if (valueString) { value = valueString; diff --git a/mozilla/extensions/wallet/src/singsign.cpp b/mozilla/extensions/wallet/src/singsign.cpp index 36e3b66ef05..ec03681e4f0 100644 --- a/mozilla/extensions/wallet/src/singsign.cpp +++ b/mozilla/extensions/wallet/src/singsign.cpp @@ -2077,31 +2077,20 @@ si_RememberSignonData PUBLIC void SINGSIGN_RememberSignonData - (nsIPrompt* dialog, const char* passwordRealm, nsVoidArray * signonData, + (nsIPrompt* dialog, nsIURI* passwordRealm, nsVoidArray * signonData, nsIDOMWindowInternal* window) { - + if (!passwordRealm) + return; nsCAutoString strippedRealm; - nsCOMPtr ioService = do_GetService(NS_IOSERVICE_CONTRACTID); - if (!ioService) return; /* Hacky security check: If address is of a scheme that - doesn't support hostnames, we have no host to get the signon data from, - so we must not attempt to restore the signon data (bug 159484) - */ - nsCOMPtr uri; - nsresult result = ioService->NewURI(nsDependentCString(passwordRealm), - nsnull, nsnull, getter_AddRefs(uri)); - if (NS_FAILED(result)) { - return; - } - nsCAutoString tempHost; - result = uri->GetHost(tempHost); - if (NS_FAILED(result)) { - return; - } + * doesn't support hostnames, we have no host to get the signon data from, + * so we must not attempt to restore the signon data (bug 159484) */ + nsresult rv = passwordRealm->GetHost(strippedRealm); + if (NS_FAILED(rv)) + return; - ioService->ExtractUrlPart(nsDependentCString(passwordRealm), nsIIOService::url_Host, strippedRealm); if (!strippedRealm.IsEmpty()) { si_RememberSignonData(dialog, strippedRealm.get(), signonData, window); } @@ -2212,29 +2201,19 @@ si_RestoreSignonData(nsIPrompt* dialog, const char* passwordRealm, const PRUnich } PUBLIC void -SINGSIGN_RestoreSignonData(nsIPrompt* dialog, const char* passwordRealm, const PRUnichar* name, PRUnichar** value, PRUint32 elementNumber) { +SINGSIGN_RestoreSignonData(nsIPrompt* dialog, nsIURI* passwordRealm, const PRUnichar* name, PRUnichar** value, PRUint32 elementNumber) { + if (!passwordRealm) + return; nsCAutoString strippedRealm; - nsCOMPtr ioService = do_GetService(NS_IOSERVICE_CONTRACTID); - if (!ioService) return; /* Hacky security check: If address is of a scheme that - doesn't support hostnames, we have no host to get the signon data from, - so we must not attempt to restore the signon data (bug 159484) - */ - nsCOMPtr uri; - nsresult result = ioService->NewURI(nsDependentCString(passwordRealm), - nsnull, nsnull, getter_AddRefs(uri)); - if (NS_FAILED(result)) { - return; - } - nsCAutoString tempHost; - result = uri->GetHost(tempHost); - if (NS_FAILED(result)) { - return; - } + * doesn't support hostnames, we have no host to get the signon data from, + * so we must not attempt to restore the signon data (bug 159484) */ + nsresult rv = passwordRealm->GetHost(strippedRealm); + if (NS_FAILED(rv)) + return; - ioService->ExtractUrlPart(nsDependentCString(passwordRealm), nsIIOService::url_Host, strippedRealm); - si_RestoreSignonData(dialog, (char*)strippedRealm.get(), name, value, elementNumber); + si_RestoreSignonData(dialog, strippedRealm.get(), name, value, elementNumber); } /* diff --git a/mozilla/extensions/wallet/src/singsign.h b/mozilla/extensions/wallet/src/singsign.h index cef88c9111e..f00688847b9 100644 --- a/mozilla/extensions/wallet/src/singsign.h +++ b/mozilla/extensions/wallet/src/singsign.h @@ -50,6 +50,8 @@ #include "nsIPref.h" #include "nsIDOMWindowInternal.h" +class nsIURI; + /* Duplicates defines as in nsIPrompt.idl -- keep in sync! */ #define SINGSIGN_SAVE_PASSWORD_NEVER 0 #define SINGSIGN_SAVE_PASSWORD_FOR_SESSION 1 @@ -78,7 +80,7 @@ extern void SINGSIGN_SignonViewerReturn(const nsString& results); extern void -SINGSIGN_RestoreSignonData(nsIPrompt* dialog, const char* passwordRealm, const PRUnichar* name, PRUnichar** value, PRUint32 elementNumber); +SINGSIGN_RestoreSignonData(nsIPrompt* dialog, nsIURI* passwordRealm, const PRUnichar* name, PRUnichar** value, PRUint32 elementNumber); extern nsresult SINGSIGN_PromptUsernameAndPassword @@ -160,7 +162,7 @@ SINGSIGN_ReencryptAll(); extern void SINGSIGN_RememberSignonData - (nsIPrompt* dialog, const char* URLName, nsVoidArray * signonData, nsIDOMWindowInternal* window); + (nsIPrompt* dialog, nsIURI* uri, nsVoidArray * signonData, nsIDOMWindowInternal* window); PR_END_EXTERN_C diff --git a/mozilla/extensions/wallet/src/wallet.cpp b/mozilla/extensions/wallet/src/wallet.cpp index a7a415b81bf..e2c08fdd706 100644 --- a/mozilla/extensions/wallet/src/wallet.cpp +++ b/mozilla/extensions/wallet/src/wallet.cpp @@ -3925,7 +3925,6 @@ WLLT_OnSubmit(nsIContent* currentForm, nsIDOMWindowInternal* window) { nsCOMPtr currentFormNode(do_QueryInterface(currentForm)); /* get url name as ascii string */ - nsCAutoString URLName; nsAutoString strippedURLNameUCS2; nsCOMPtr doc; currentForm->GetDocument(*getter_AddRefs(doc)); @@ -3937,7 +3936,6 @@ WLLT_OnSubmit(nsIContent* currentForm, nsIDOMWindowInternal* window) { if (!docURL || wallet_IsFromCartman(docURL)) { return; } - (void)docURL->GetSpec(URLName); wallet_GetHostFile(docURL, strippedURLNameUCS2); nsCAutoString strippedURLNameUTF8 = NS_ConvertUCS2toUTF8(strippedURLNameUCS2); @@ -4140,7 +4138,7 @@ WLLT_OnSubmit(nsIContent* currentForm, nsIDOMWindowInternal* window) { wwatch->GetNewPrompter(0, getter_AddRefs(dialog)); if (dialog) { - SINGSIGN_RememberSignonData(dialog, (char*)URLName.get(), signonData, window); + SINGSIGN_RememberSignonData(dialog, docURL, signonData, window); } } PRInt32 count2 = signonData->Count(); diff --git a/mozilla/mailnews/base/src/nsMessengerMigrator.cpp b/mozilla/mailnews/base/src/nsMessengerMigrator.cpp index b51dd4d82e2..deedbb4d7ce 100644 --- a/mozilla/mailnews/base/src/nsMessengerMigrator.cpp +++ b/mozilla/mailnews/base/src/nsMessengerMigrator.cpp @@ -40,7 +40,7 @@ #include "nsIComponentManager.h" #include "nsIServiceManager.h" #include "nsISupportsArray.h" -#include "nsIIOService.h" +#include "nsNetUtil.h" #include "prmem.h" #include "plstr.h" @@ -965,13 +965,14 @@ nsMessengerMigrator::Convert4XUri(const char *old_uri, PRBool for_news, const ch nsXPIDLCString hostname; nsXPIDLCString username; - nsCOMPtr ioService = do_GetService(NS_IOSERVICE_CONTRACTID); - if (!ioService) return NS_ERROR_FAILURE; - - rv = ioService->ExtractUrlPart(nsDependentCString(old_uri), nsIIOService::url_Host, hostname); + nsCOMPtr uri; + rv = NS_NewURI(getter_AddRefs(uri), nsDependentCString(old_uri)); if (NS_FAILED(rv)) return rv; - rv = ioService->ExtractUrlPart(nsDependentCString(old_uri), nsIIOService::url_Username, username); + rv = uri->GetHost(hostname); + if (NS_FAILED(rv)) return rv; + + rv = uri->GetUsername(username); if (NS_FAILED(rv)) return rv; // in 4.x, mac and windows stored the URI as IMAP:// diff --git a/mozilla/mailnews/compose/src/nsMsgAttachment.cpp b/mozilla/mailnews/compose/src/nsMsgAttachment.cpp index 3d5428666e7..fed1616b95c 100644 --- a/mozilla/mailnews/compose/src/nsMsgAttachment.cpp +++ b/mozilla/mailnews/compose/src/nsMsgAttachment.cpp @@ -175,33 +175,23 @@ NS_IMETHODIMP nsMsgAttachment::EqualsUrl(nsIMsgAttachment *attachment, PRBool *_ nsresult nsMsgAttachment::DeleteAttachment() { - nsresult rv; + nsresult rv; PRBool isAFile = PR_FALSE; - nsCOMPtr urlFile(do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv)); + nsCOMPtr urlFile; + rv = NS_GetFileFromURLSpec(mUrl, getter_AddRefs(urlFile)); + NS_ASSERTION(NS_SUCCEEDED(rv), "Can't nsIFile from URL string"); if (NS_SUCCEEDED(rv)) - { - NS_InitFileFromURLSpec(urlFile, mUrl); - if (NS_SUCCEEDED(rv)) - { - PRBool bExists = PR_FALSE; - rv = urlFile->Exists(&bExists); - if (NS_FAILED(rv)) - {NS_ASSERTION(0, "bExists() call failed!");} - else - if (bExists) - { - rv = urlFile->IsFile(&isAFile); - if (NS_FAILED(rv)) - {NS_ASSERTION(0, "IsFile() call failed!");} - } + { + PRBool bExists = PR_FALSE; + rv = urlFile->Exists(&bExists); + NS_ASSERTION(NS_SUCCEEDED(rv), "Exists() call failed!"); + if (NS_SUCCEEDED(rv) && bExists) + { + rv = urlFile->IsFile(&isAFile); + NS_ASSERTION(NS_SUCCEEDED(rv), "IsFile() call failed!"); } - else - {NS_ASSERTION(0, "Can't set url in nsILocalFile interface");} } - else - {NS_ASSERTION(0, "Can't creat nsIFileURL interface");} - // remove it if it's a valid file if (isAFile) diff --git a/mozilla/modules/libjar/nsJARURI.cpp b/mozilla/modules/libjar/nsJARURI.cpp index aeacfe46c7e..9d57b3512ab 100644 --- a/mozilla/modules/libjar/nsJARURI.cpp +++ b/mozilla/modules/libjar/nsJARURI.cpp @@ -25,6 +25,7 @@ #include "nsIServiceManager.h" #include "nsIZipReader.h" #include "nsReadableUtils.h" +#include "nsURLHelper.h" //////////////////////////////////////////////////////////////////////////////// @@ -91,11 +92,11 @@ NS_IMETHODIMP nsJARURI::SetSpec(const nsACString &aSpec) { nsresult rv; - nsCOMPtr serv(do_GetIOService(&rv)); + nsCOMPtr ioServ(do_GetIOService(&rv)); if (NS_FAILED(rv)) return rv; nsCAutoString scheme; - rv = serv->ExtractScheme(aSpec, scheme); + rv = ::ExtractURLScheme(aSpec, nsnull, nsnull, &scheme); if (NS_FAILED(rv)) return rv; if (strcmp("jar", scheme.get()) != 0) @@ -119,16 +120,16 @@ nsJARURI::SetSpec(const nsACString &aSpec) begin.advance(4); - rv = serv->NewURI(Substring(begin, delim_begin), mCharsetHint.get(), nsnull, getter_AddRefs(mJARFile)); + rv = ioServ->NewURI(Substring(begin, delim_begin), mCharsetHint.get(), nsnull, getter_AddRefs(mJARFile)); if (NS_FAILED(rv)) return rv; // skip over any extra '/' chars while (*delim_end == '/') ++delim_end; - rv = serv->ResolveRelativePath(Substring(delim_end, end), - NS_LITERAL_CSTRING(""), - mJAREntry); + rv = ::ResolveRelativePath(Substring(delim_end, end), + NS_LITERAL_CSTRING(""), + mJAREntry); return rv; } @@ -327,11 +328,8 @@ nsJARURI::Resolve(const nsACString &relativePath, nsACString &result) { nsresult rv; - nsCOMPtr serv(do_GetIOService(&rv)); - if (NS_FAILED(rv)) return rv; - nsCAutoString scheme; - rv = serv->ExtractScheme(relativePath, scheme); + rv = ::ExtractURLScheme(relativePath, nsnull, nsnull, &scheme); if (NS_SUCCEEDED(rv)) { // then aSpec is absolute result = relativePath; @@ -346,8 +344,8 @@ nsJARURI::Resolve(const nsACString &relativePath, nsACString &result) path = ""; nsCAutoString resolvedEntry; - rv = serv->ResolveRelativePath(relativePath, path, - resolvedEntry); + rv = ::ResolveRelativePath(relativePath, path, + resolvedEntry); if (NS_FAILED(rv)) return rv; return FormatSpec(resolvedEntry, result); @@ -385,13 +383,11 @@ nsJARURI::GetJAREntry(nsACString &entryPath) NS_IMETHODIMP nsJARURI::SetJAREntry(const nsACString &entryPath) { - nsresult rv; - nsCOMPtr serv(do_GetIOService(&rv)); - if (NS_FAILED(rv)) return rv; - mJAREntry.Truncate(); - return serv->ResolveRelativePath(entryPath, NS_LITERAL_CSTRING(""), mJAREntry); + return ::ResolveRelativePath(entryPath, + NS_LITERAL_CSTRING(""), + mJAREntry); } //////////////////////////////////////////////////////////////////////////////// diff --git a/mozilla/modules/plugin/base/src/nsPluginHostImpl.cpp b/mozilla/modules/plugin/base/src/nsPluginHostImpl.cpp index 233964c5c1b..fb7b46a166c 100644 --- a/mozilla/modules/plugin/base/src/nsPluginHostImpl.cpp +++ b/mozilla/modules/plugin/base/src/nsPluginHostImpl.cpp @@ -6343,15 +6343,23 @@ nsPluginHostImpl::CreateTmpFileToPost(const char *postDataURL, char **pTmpFileNa nsresult rv; PRInt64 fileSize; nsCAutoString filename; + // stat file == get size & convert file:///c:/ to c: if needed - nsCOMPtr inFile = do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv); - if (NS_FAILED(rv) || - (NS_FAILED(rv = NS_InitFileFromURLSpec(inFile, nsDependentCString(postDataURL))) && - NS_FAILED(rv = inFile->InitWithNativePath(nsDependentCString(postDataURL)))) || - NS_FAILED(rv = inFile->GetFileSize(&fileSize)) || - NS_FAILED(rv = inFile->GetNativePath(filename)) - ) - return rv; + nsCOMPtr inFile; + rv = NS_GetFileFromURLSpec(nsDependentCString(postDataURL), + getter_AddRefs(inFile)); + if (NS_FAILED(rv)) { + nsCOMPtr localFile; + rv = NS_NewNativeLocalFile(nsDependentCString(postDataURL), PR_FALSE, + getter_AddRefs(localFile)); + if (NS_FAILED(rv)) return rv; + inFile = localFile; + } + rv = inFile->GetFileSize(&fileSize); + if (NS_FAILED(rv)) return rv; + rv = inFile->GetNativePath(filename); + if (NS_FAILED(rv)) return rv; + if (!LL_IS_ZERO(fileSize)) { nsCOMPtr inStream; rv = NS_NewLocalFileInputStream(getter_AddRefs(inStream), inFile); diff --git a/mozilla/netwerk/base/public/nsIIOService.idl b/mozilla/netwerk/base/public/nsIIOService.idl index 0e11c401a9e..3a021f0c6c2 100644 --- a/mozilla/netwerk/base/public/nsIIOService.idl +++ b/mozilla/netwerk/base/public/nsIIOService.idl @@ -40,33 +40,48 @@ interface nsIProtocolHandler; interface nsIChannel; interface nsIURI; -interface nsIURLParser; interface nsIFile; -[scriptable, uuid(ab7c3a84-d488-11d3-8cda-0060b0fc14a3)] +/** + * nsIIOService provides a set of utility functions for necko. The interface + * duplicates many of the nsIProtocolHandler methods in a protocol handler + * independent way (e.g., NewURI sniffs the scheme to determine which protocol + * handler's NewURI should be invoked). nsIIOService also provides a set of + * URL parsing utility functions. These are provided to make the programmers + * job easier and in some cases to improve performance by eliminating + * intermediate data structures and interfaces. + * + * @status UNDER_REVIEW + */ +[scriptable, uuid(bddeda3f-9020-4d12-8c70-984ee9f7935e)] interface nsIIOService : nsISupports { + /************************************************************************* + * Protocol handler utilities + */ + /** * Returns a protocol handler for a given URI scheme. * - * @param scheme URI scheme - * @return reference to nsIProtcolHandler + * @param aScheme the URI scheme + * @return reference to corresponding nsIProtocolHandler */ - nsIProtocolHandler getProtocolHandler(in string scheme); + nsIProtocolHandler getProtocolHandler(in string aScheme); /** * Returns the protocol flags for a given scheme. * - * @param scheme URI scheme - * @return returns unsigned long for protocol flags + * @param aScheme the URI scheme + * @return value of corresponding nsIProtocolHandler::protocolFlags */ - unsigned long getProtocolFlags(in string scheme); + unsigned long getProtocolFlags(in string aScheme); /** * This method constructs a new URI by first determining the scheme * of the URI spec, and then delegating the construction of the URI * to the protocol handler for that scheme. QueryInterface can be used * on the resulting URI object to obtain a more specific type of URI. + * * @see nsIProtocolHandler::newURI */ nsIURI newURI(in AUTF8String aSpec, @@ -74,32 +89,23 @@ interface nsIIOService : nsISupports in nsIURI aBaseURI); /** - * This method constructs a new file URI + * This method constructs a new URI from a nsIFile. * - * @param aFile nsIFile from which to make an URI from + * @param aFile specifies the file path * @return reference to a new nsIURI object */ nsIURI newFileURI(in nsIFile aFile); /** - * Creates a channel for a given URI. The notificationCallbacks argument - * is used to obtain the appropriate callbacks for the URI's protocol - * from the application. + * Creates a channel for a given URI. * - * @param originalURI - Specifies the original URI which caused the - * creation of this channel. This can occur when the construction of - * one channel (e.g. for resource:) causes another channel to be created - * on its behalf (e.g. a file: channel), or if a redirect occurs, - * causing the current URL to become different from the original URL. - * If NULL, the aURI parameter will be used as the originalURI instead. - * - * @param aURI nsIURI to make a channel from - * @return a reference to the new nsIChannel object + * @param aURI nsIURI from which to make a channel + * @return reference to the new nsIChannel object */ nsIChannel newChannelFromURI(in nsIURI aURI); /** - * Shortcut equivalent to newChannelFromURI(newURI(...)) + * Equivalent to newChannelFromURI(newURI(...)) */ nsIChannel newChannel(in AUTF8String aSpec, in string aOriginCharset, @@ -115,91 +121,56 @@ interface nsIIOService : nsISupports attribute boolean offline; /** - * Checks if a port number is banned. + * Checks if a port number is banned. This involves consulting a black- + * list of port numbers corresponding to services that may be easily + * exploitable. If the given port is found on the black-list, then the + * protocol handler (corresponding to aScheme) will be asked if it wishes + * to override the IO service's decision to block the port. This gives + * the protocol handler ultimate control over its own security policy + * while ensuring reasonable, default protection. * - * |allowPort| will check a list of "known-to-do-bad-things" - * port numbers. If the given port is found on the blacklist, - * |allowPort| will ask the protocol handler if it wishes to override. - * Scheme can be null. + * @see nsIProtocolHandler::allowPort */ - boolean allowPort(in long port, in string scheme); + boolean allowPort(in long aPort, in string aScheme); - //////////////////////////////////////////////////////////////////////////// - // URL parsing utilities + + /************************************************************************* + * URL parsing utilities + * + * The set of methods provided here is intentionally limited. Most of + * Necko's URL parsing functionality is provided via nsIURI. This is + * done to ensure a common point of access to URL parsing, which is + * protocol dependent. Do not try to parse a random URL string as the + * result will likely differ (perhaps only in some subtle way) from the + * value of the corresponding nsIURI attribute. Violating this rule + * may make your code vulnerable to various kinds of security exploits. + * DON'T RISK IT! --darin + */ /** - * Utility for protocol implementors -- extracts the scheme from a URL - * string, consistently and according to spec. + * Utility to extract the scheme from a URL string, consistently and + * according to spec (see RFC 2396). * - * @param urlString the URL string to parse - * @return scheme + * @param aSpec the URL string to parse + * @return URL scheme * - * @throws NS_ERROR_MALFORMED_URI if urlString is not of the right form. + * @throws NS_ERROR_MALFORMED_URI if URL string is not of the right form. */ ACString extractScheme(in AUTF8String urlString); /** - * returns the protocol specific URL parser - */ - nsIURLParser getParserForScheme(in string scheme); - - /** - * @param urlString absolute URL path. - * @param flags combination of url_XXX flags. + * Converts the nsIFile to the corresponding URL string. NOTE: under + * some platforms this is a lossy conversion (e.g., Mac Carbon build). + * If the nsIFile is a local file, then the result will be a file:// + * URL string. * - * @throws NS_ERROR_MALFORMED_URI if urlString is not of the right form. - */ - AUTF8String extractUrlPart(in AUTF8String urlString, in short flags); - - /** - * Constants for the mask in the call to extractUrlPart - * - * XXX these should really be enumerated in left-to-right order! - */ - const short url_Scheme = (1<<0); - const short url_Username = (1<<1); - const short url_Password = (1<<2); - const short url_Host = (1<<3); - const short url_Directory = (1<<4); - const short url_FileBaseName = (1<<5); - const short url_FileExtension = (1<<6); - const short url_Param = (1<<7); - const short url_Query = (1<<8); - const short url_Ref = (1<<9); - const short url_Path = (1<<10); - const short url_Port = (1<<11); - - /** - * Resolves a relative path string containing "." and ".." - * with respect to a base path (assumed to already be resolved). - * For example, resolving "../../foo/./bar/../baz.html" w.r.t. - * "/a/b/c/d/e/" yields "/a/b/c/foo/baz.html". Attempting to - * ascend above the base results in the NS_ERROR_MALFORMED_URI - * exception. If basePath is null, it treats it as "/". - * - * @param relativePath a relative URI - * @param basePath a base URI - * - * @return a new string, representing canonical uri - */ - AUTF8String resolveRelativePath(in AUTF8String relativePath, - in AUTF8String basePath); - - /** - * conversions between nsILocalFile and a file url string - */ - - /** - * gets a file:// url out of an nsIFile - * @param file the file to extract the path from - * @return a file:// style url string + * The resulting string may contain URL-escaped characters. */ AUTF8String getURLSpecFromFile(in nsIFile file); /** - * Sets the native path of the file given the url string - * @param file the file to initialize with the given spec - * @param url the url string which will be used to initialize the file + * Converts the URL string into the corresponding nsIFile if possible. + * A local file will be created if the URL string begins with file://. */ - void initFileFromURLSpec(in nsIFile file, in AUTF8String url); + nsIFile getFileFromURLSpec(in AUTF8String url); }; diff --git a/mozilla/netwerk/base/public/nsNetUtil.h b/mozilla/netwerk/base/public/nsNetUtil.h index d8dd661f954..76b7bae4fdc 100644 --- a/mozilla/netwerk/base/public/nsNetUtil.h +++ b/mozilla/netwerk/base/public/nsNetUtil.h @@ -739,8 +739,8 @@ NS_NewProxyInfo(const char* type, const char* host, PRInt32 port, nsIProxyInfo* } inline nsresult -NS_InitFileFromURLSpec(nsIFile* aFile, const nsACString &inURL, - nsIIOService *ioService=nsnull) +NS_GetFileFromURLSpec(const nsACString &inURL, nsIFile **result, + nsIIOService *ioService=nsnull) { nsCOMPtr serv; if (ioService == nsnull) { @@ -749,7 +749,7 @@ NS_InitFileFromURLSpec(nsIFile* aFile, const nsACString &inURL, if (NS_FAILED(rv)) return rv; ioService = serv.get(); } - return ioService->InitFileFromURLSpec(aFile, inURL); + return ioService->GetFileFromURLSpec(inURL, result); } inline nsresult diff --git a/mozilla/netwerk/base/src/nsIOService.cpp b/mozilla/netwerk/base/src/nsIOService.cpp index 73ee8528d11..3d4944950ab 100644 --- a/mozilla/netwerk/base/src/nsIOService.cpp +++ b/mozilla/netwerk/base/src/nsIOService.cpp @@ -479,8 +479,7 @@ nsIOService::ExtractScheme(const nsACString &inURI, nsACString &scheme) return ExtractURLScheme(inURI, nsnull, nsnull, &scheme); } -/* nsIURLParser getParserForScheme (in string scheme); */ -NS_IMETHODIMP +NS_METHOD nsIOService::GetParserForScheme(const char *scheme, nsIURLParser **_retval) { nsresult rv; @@ -549,6 +548,7 @@ nsIOService::GetParserForScheme(const char *scheme, nsIURLParser **_retval) return NS_OK; } +#if 0 static inline void ExtractUrlPart_Helper(const nsACString &src, PRUint32 pos, PRInt32 len, nsACString &result) { @@ -706,6 +706,7 @@ nsIOService::ExtractUrlPart(const nsACString &urlString, PRInt16 flag, nsACStrin } return NS_OK; } +#endif NS_IMETHODIMP nsIOService::GetProtocolFlags(const char* scheme, PRUint32 *flags) @@ -904,77 +905,6 @@ nsIOService::AllowPort(PRInt32 inPort, const char *scheme, PRBool *_retval) *_retval = PR_TRUE; return NS_OK; } -//////////////////////////////////////////////////////////////////////////////// -// URL parsing utilities - -NS_IMETHODIMP -nsIOService::ResolveRelativePath(const nsACString &relativePath, const nsACString &basePath, - nsACString &result) -{ - nsCAutoString name; - nsCAutoString path(basePath); - PRBool needsDelim = PR_FALSE; - - if ( !path.IsEmpty() ) { - PRUnichar last = path.Last(); - needsDelim = !(last == '/' || last == '\\' ); - } - - nsACString::const_iterator beg, end; - relativePath.BeginReading(beg); - relativePath.EndReading(end); - - PRBool stop = PR_FALSE; - char c; - for (; !stop; ++beg) { - c = (beg == end) ? '\0' : *beg; - //printf("%c [name=%s] [path=%s]\n", c, name.get(), path.get()); - switch (c) { - case '\0': - case '#': - case ';': - case '?': - stop = PR_TRUE; - // fall through... - case '/': - case '\\': - // delimiter found - if (name.Equals("..")) { - // pop path - // If we already have the delim at end, then - // skip over that when searching for next one to the left - PRInt32 offset = path.Length() - (needsDelim ? 1 : 2); - PRInt32 pos = path.RFind("/", PR_FALSE, offset); - if (pos > 0) - path.Truncate(pos + 1); - else - return NS_ERROR_MALFORMED_URI; - } - else if (name.Equals(".") || name.Equals("")) { - // do nothing - } - else { - // append name to path - if (needsDelim) - path += "/"; - path += name; - needsDelim = PR_TRUE; - } - name = ""; - break; - - default: - // append char to name - name += c; - } - } - // append anything left on relativePath (e.g. #..., ;..., ?...) - if (c != '\0') - path += Substring(--beg, end); - - result = path; - return NS_OK; -} //////////////////////////////////////////////////////////////////////////////// diff --git a/mozilla/netwerk/base/src/nsIOService.h b/mozilla/netwerk/base/src/nsIOService.h index ca4c6fa1fbb..3259d13d30c 100644 --- a/mozilla/netwerk/base/src/nsIOService.h +++ b/mozilla/netwerk/base/src/nsIOService.h @@ -100,6 +100,8 @@ protected: NS_METHOD CacheURLParser(const char *scheme, nsIURLParser* hdlr); + NS_METHOD GetParserForScheme(const char *scheme, nsIURLParser **); + // Prefs wrangling void PrefsChanged(nsIPrefBranch *prefs, const char *pref = nsnull); void GetPrefBranch(nsIPrefBranch **); diff --git a/mozilla/netwerk/base/src/nsIOServiceMac.cpp b/mozilla/netwerk/base/src/nsIOServiceMac.cpp index 81b66756c9f..f23422d01fa 100644 --- a/mozilla/netwerk/base/src/nsIOServiceMac.cpp +++ b/mozilla/netwerk/base/src/nsIOServiceMac.cpp @@ -110,12 +110,12 @@ nsIOService::GetURLSpecFromFile(nsIFile *aFile, nsACString &aURL) } NS_IMETHODIMP -nsIOService::InitFileFromURLSpec(nsIFile *aFile, const nsACString &aURL) +nsIOService::GetFileFromURLSpec(const nsACString &aURL, nsIFile **result) { nsresult rv; - NS_ENSURE_ARG(aFile); - nsCOMPtr localFile = do_QueryInterface(aFile, &rv); + nsCOMPtr localFile( + do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv)); if (NS_FAILED(rv)) { NS_ERROR("Only nsILocalFile supported right now"); return rv; @@ -152,5 +152,9 @@ nsIOService::InitFileFromURLSpec(nsIFile *aFile, const nsACString &aURL) path.Cut(0, 1); // assuming path is encoded in the native charset - return localFile->InitWithNativePath(path); + rv = localFile->InitWithNativePath(path); + if (NS_FAILED(rv)) return rv; + + NS_ADDREF(*result = localFile); + return NS_OK; } diff --git a/mozilla/netwerk/base/src/nsIOServiceOS2.cpp b/mozilla/netwerk/base/src/nsIOServiceOS2.cpp index c0b57e3b6bb..55f3f6ec386 100644 --- a/mozilla/netwerk/base/src/nsIOServiceOS2.cpp +++ b/mozilla/netwerk/base/src/nsIOServiceOS2.cpp @@ -96,11 +96,12 @@ nsIOService::GetURLSpecFromFile(nsIFile *aFile, nsACString &result) } NS_IMETHODIMP -nsIOService::InitFileFromURLSpec(nsIFile *aFile, const nsACString &aURL) +nsIOService::GetFileFromURLSpec(const nsACString &aURL, nsIFile **result) { nsresult rv; - nsCOMPtr localFile = do_QueryInterface(aFile, &rv); + nsCOMPtr localFile( + do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv)); if (NS_FAILED(rv)) { NS_ERROR("Only nsILocalFile supported right now"); return rv; @@ -133,7 +134,11 @@ nsIOService::InitFileFromURLSpec(nsIFile *aFile, const nsACString &aURL) path.Cut(0, 1); // assuming path is encoded in the native charset - return localFile->InitWithNativePath(path); + rv = localFile->InitWithNativePath(path); + if (NS_FAILED(rv)) return rv; + + NS_ADDREF(*result = localFile); + return NS_OK; } static int isleadbyte(int c) diff --git a/mozilla/netwerk/base/src/nsIOServiceUnix.cpp b/mozilla/netwerk/base/src/nsIOServiceUnix.cpp index 96d00a1b6eb..b1d15e3eac4 100644 --- a/mozilla/netwerk/base/src/nsIOServiceUnix.cpp +++ b/mozilla/netwerk/base/src/nsIOServiceUnix.cpp @@ -80,11 +80,12 @@ nsIOService::GetURLSpecFromFile(nsIFile *aFile, nsACString &result) } NS_IMETHODIMP -nsIOService::InitFileFromURLSpec(nsIFile* aFile, const nsACString &aURL) +nsIOService::GetFileFromURLSpec(const nsACString &aURL, nsIFile **result) { nsresult rv; - nsCOMPtr localFile = do_QueryInterface(aFile, &rv); + nsCOMPtr localFile( + do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv)); if (NS_FAILED(rv)) { NS_ERROR("Only nsILocalFile supported right now"); return rv; @@ -107,5 +108,9 @@ nsIOService::InitFileFromURLSpec(nsIFile* aFile, const nsACString &aURL) NS_UnescapeURL(path); // assuming path is encoded in the native charset - return localFile->InitWithNativePath(path); + rv = localFile->InitWithNativePath(path); + if (NS_FAILED(rv)) return rv; + + NS_ADDREF(*result = localFile); + return NS_OK; } diff --git a/mozilla/netwerk/base/src/nsIOServiceWin.cpp b/mozilla/netwerk/base/src/nsIOServiceWin.cpp index 7f1e3eda4e0..182f19613b7 100644 --- a/mozilla/netwerk/base/src/nsIOServiceWin.cpp +++ b/mozilla/netwerk/base/src/nsIOServiceWin.cpp @@ -93,11 +93,12 @@ nsIOService::GetURLSpecFromFile(nsIFile *aFile, nsACString &result) } NS_IMETHODIMP -nsIOService::InitFileFromURLSpec(nsIFile *aFile, const nsACString &aURL) +nsIOService::GetFileFromURLSpec(const nsACString &aURL, nsIFile **result) { nsresult rv; - nsCOMPtr localFile = do_QueryInterface(aFile, &rv); + nsCOMPtr localFile( + do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv)); if (NS_FAILED(rv)) { NS_ERROR("Only nsILocalFile supported right now"); return rv; @@ -130,5 +131,9 @@ nsIOService::InitFileFromURLSpec(nsIFile *aFile, const nsACString &aURL) path.Cut(0, 1); // assuming path is encoded in the native charset - return localFile->InitWithNativePath(path); + rv = localFile->InitWithNativePath(path); + if (NS_FAILED(rv)) return rv; + + NS_ADDREF(*result = localFile); + return NS_OK; } diff --git a/mozilla/netwerk/base/src/nsStandardURL.cpp b/mozilla/netwerk/base/src/nsStandardURL.cpp index 498c2596b7b..403c749c6e1 100644 --- a/mozilla/netwerk/base/src/nsStandardURL.cpp +++ b/mozilla/netwerk/base/src/nsStandardURL.cpp @@ -57,6 +57,7 @@ static NS_DEFINE_CID(kThisImplCID, NS_THIS_STANDARDURL_IMPL_CID); static NS_DEFINE_CID(kStandardURLCID, NS_STANDARDURL_CID); +nsIIOService *nsStandardURL::gIOService = nsnull; nsIURLParser *nsStandardURL::gNoAuthParser = nsnull; nsIURLParser *nsStandardURL::gAuthParser = nsnull; nsIURLParser *nsStandardURL::gStdParser = nsnull; @@ -364,6 +365,13 @@ nsStandardURL::InitGlobalObjects() NS_ADDREF(gStdParser); } + nsCOMPtr serv(do_GetIOService()); + NS_ASSERTION(serv, "failed getting IO service"); + if (serv) { + gIOService = serv.get(); + NS_ADDREF(gIOService); + } + nsCOMPtr prefService( do_GetService(NS_PREFSERVICE_CONTRACTID) ); if (prefService) { nsCOMPtr prefBranch; @@ -382,6 +390,7 @@ nsStandardURL::InitGlobalObjects() void nsStandardURL::ShutdownGlobalObjects() { + NS_IF_RELEASE(gIOService); NS_IF_RELEASE(gNoAuthParser); NS_IF_RELEASE(gAuthParser); NS_IF_RELEASE(gStdParser); @@ -2173,23 +2182,27 @@ nsStandardURL::GetFile(nsIFile **result) return NS_ERROR_FAILURE; } - nsresult rv; - nsCOMPtr localFile(do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv)); - if (NS_FAILED(rv)) return rv; - - rv = NS_InitFileFromURLSpec(localFile, mSpec); - if (NS_FAILED(rv)) return rv; - + nsresult rv = gIOService->GetFileFromURLSpec(mSpec, result); + + // XXX ultimately, we should probably cache this result to speed + // up subsequent calls, but past attempts to do so have been + // met with nasty smoketest blockers (e.g., see bug 161921). + // it seems that some folks like to modify the returned nsIFile, + // so we'd have to clone the result before handing it out. + // unfortunately, there are bugs in the implementation(s) of + // nsIFile::clone that prevent us from using this as a solution + // right now (see bug 122892). + #if defined(PR_LOGGING) - if (LOG_ENABLED()) { + if (NS_SUCCEEDED(rv) && LOG_ENABLED()) { nsCAutoString path; - localFile->GetNativePath(path); + (*result)->GetNativePath(path); LOG(("nsStandardURL::GetFile [this=%p spec=%s resulting_path=%s]\n", this, mSpec.get(), path.get())); } #endif - return CallQueryInterface(localFile, result); + return rv; } NS_IMETHODIMP @@ -2202,7 +2215,7 @@ nsStandardURL::SetFile(nsIFile *file) nsresult rv; nsCAutoString url; - rv = NS_GetURLSpecFromFile(file, url); + rv = gIOService->GetURLSpecFromFile(file, url); if (NS_FAILED(rv)) return rv; rv = SetSpec(url); diff --git a/mozilla/netwerk/base/src/nsStandardURL.h b/mozilla/netwerk/base/src/nsStandardURL.h index 4c162da282c..585208ce1f4 100644 --- a/mozilla/netwerk/base/src/nsStandardURL.h +++ b/mozilla/netwerk/base/src/nsStandardURL.h @@ -49,6 +49,7 @@ #include "nsIURLParser.h" #include "nsIUnicodeEncoder.h" #include "nsIObserver.h" +#include "nsIIOService.h" #include "nsCOMPtr.h" class nsIBinaryInputStream; @@ -239,6 +240,7 @@ private: // global objects. don't use COMPtr as its destructor will cause a // coredump if we leak it. + static nsIIOService *gIOService; static nsIURLParser *gNoAuthParser; static nsIURLParser *gAuthParser; static nsIURLParser *gStdParser; diff --git a/mozilla/netwerk/base/src/nsURLHelper.cpp b/mozilla/netwerk/base/src/nsURLHelper.cpp index 9cc9ff3344a..93fb38a622f 100644 --- a/mozilla/netwerk/base/src/nsURLHelper.cpp +++ b/mozilla/netwerk/base/src/nsURLHelper.cpp @@ -355,3 +355,73 @@ IsValidScheme(const char *scheme, PRUint32 schemeLen) return PR_TRUE; } + +nsresult +ResolveRelativePath(const nsACString &relativePath, + const nsACString &basePath, + nsACString &result) +{ + nsCAutoString name; + nsCAutoString path(basePath); + PRBool needsDelim = PR_FALSE; + + if ( !path.IsEmpty() ) { + PRUnichar last = path.Last(); + needsDelim = !(last == '/' || last == '\\' ); + } + + nsACString::const_iterator beg, end; + relativePath.BeginReading(beg); + relativePath.EndReading(end); + + PRBool stop = PR_FALSE; + char c; + for (; !stop; ++beg) { + c = (beg == end) ? '\0' : *beg; + //printf("%c [name=%s] [path=%s]\n", c, name.get(), path.get()); + switch (c) { + case '\0': + case '#': + case ';': + case '?': + stop = PR_TRUE; + // fall through... + case '/': + case '\\': + // delimiter found + if (name.Equals("..")) { + // pop path + // If we already have the delim at end, then + // skip over that when searching for next one to the left + PRInt32 offset = path.Length() - (needsDelim ? 1 : 2); + PRInt32 pos = path.RFind("/", PR_FALSE, offset); + if (pos > 0) + path.Truncate(pos + 1); + else + return NS_ERROR_MALFORMED_URI; + } + else if (name.Equals(".") || name.Equals("")) { + // do nothing + } + else { + // append name to path + if (needsDelim) + path += "/"; + path += name; + needsDelim = PR_TRUE; + } + name = ""; + break; + + default: + // append char to name + name += c; + } + } + // append anything left on relativePath (e.g. #..., ;..., ?...) + if (c != '\0') + path += Substring(--beg, end); + + result = path; + return NS_OK; +} diff --git a/mozilla/netwerk/base/src/nsURLHelper.h b/mozilla/netwerk/base/src/nsURLHelper.h index 41f6bc1524e..b763f1c349b 100644 --- a/mozilla/netwerk/base/src/nsURLHelper.h +++ b/mozilla/netwerk/base/src/nsURLHelper.h @@ -74,4 +74,21 @@ inline PRBool IsValidScheme(const nsAFlatCString &scheme) return IsValidScheme(scheme.get(), scheme.Length()); } +/** + * Resolves a relative path string containing "." and ".." + * with respect to a base path (assumed to already be resolved). + * For example, resolving "../../foo/./bar/../baz.html" w.r.t. + * "/a/b/c/d/e/" yields "/a/b/c/foo/baz.html". Attempting to + * ascend above the base results in the NS_ERROR_MALFORMED_URI + * exception. If basePath is null, it treats it as "/". + * + * @param relativePath a relative URI + * @param basePath a base URI + * + * @return a new string, representing canonical uri + */ +nsresult ResolveRelativePath(const nsACString &relativePath, + const nsACString &basePath, + nsACString &result); + #endif diff --git a/mozilla/netwerk/protocol/jar/src/Makefile.in b/mozilla/netwerk/protocol/jar/src/Makefile.in index fe7d5b6340b..5748574a837 100644 --- a/mozilla/netwerk/protocol/jar/src/Makefile.in +++ b/mozilla/netwerk/protocol/jar/src/Makefile.in @@ -45,6 +45,9 @@ CPPSRCS = \ nsJARURI.cpp \ $(NULL) +LOCAL_INCLUDES = \ + -I$(srcdir)/../../../base/src + # we don't want the shared lib, but we want to force the creation of a # static lib. FORCE_STATIC_LIB = 1 diff --git a/mozilla/netwerk/protocol/jar/src/nsJARURI.cpp b/mozilla/netwerk/protocol/jar/src/nsJARURI.cpp index aeacfe46c7e..9d57b3512ab 100644 --- a/mozilla/netwerk/protocol/jar/src/nsJARURI.cpp +++ b/mozilla/netwerk/protocol/jar/src/nsJARURI.cpp @@ -25,6 +25,7 @@ #include "nsIServiceManager.h" #include "nsIZipReader.h" #include "nsReadableUtils.h" +#include "nsURLHelper.h" //////////////////////////////////////////////////////////////////////////////// @@ -91,11 +92,11 @@ NS_IMETHODIMP nsJARURI::SetSpec(const nsACString &aSpec) { nsresult rv; - nsCOMPtr serv(do_GetIOService(&rv)); + nsCOMPtr ioServ(do_GetIOService(&rv)); if (NS_FAILED(rv)) return rv; nsCAutoString scheme; - rv = serv->ExtractScheme(aSpec, scheme); + rv = ::ExtractURLScheme(aSpec, nsnull, nsnull, &scheme); if (NS_FAILED(rv)) return rv; if (strcmp("jar", scheme.get()) != 0) @@ -119,16 +120,16 @@ nsJARURI::SetSpec(const nsACString &aSpec) begin.advance(4); - rv = serv->NewURI(Substring(begin, delim_begin), mCharsetHint.get(), nsnull, getter_AddRefs(mJARFile)); + rv = ioServ->NewURI(Substring(begin, delim_begin), mCharsetHint.get(), nsnull, getter_AddRefs(mJARFile)); if (NS_FAILED(rv)) return rv; // skip over any extra '/' chars while (*delim_end == '/') ++delim_end; - rv = serv->ResolveRelativePath(Substring(delim_end, end), - NS_LITERAL_CSTRING(""), - mJAREntry); + rv = ::ResolveRelativePath(Substring(delim_end, end), + NS_LITERAL_CSTRING(""), + mJAREntry); return rv; } @@ -327,11 +328,8 @@ nsJARURI::Resolve(const nsACString &relativePath, nsACString &result) { nsresult rv; - nsCOMPtr serv(do_GetIOService(&rv)); - if (NS_FAILED(rv)) return rv; - nsCAutoString scheme; - rv = serv->ExtractScheme(relativePath, scheme); + rv = ::ExtractURLScheme(relativePath, nsnull, nsnull, &scheme); if (NS_SUCCEEDED(rv)) { // then aSpec is absolute result = relativePath; @@ -346,8 +344,8 @@ nsJARURI::Resolve(const nsACString &relativePath, nsACString &result) path = ""; nsCAutoString resolvedEntry; - rv = serv->ResolveRelativePath(relativePath, path, - resolvedEntry); + rv = ::ResolveRelativePath(relativePath, path, + resolvedEntry); if (NS_FAILED(rv)) return rv; return FormatSpec(resolvedEntry, result); @@ -385,13 +383,11 @@ nsJARURI::GetJAREntry(nsACString &entryPath) NS_IMETHODIMP nsJARURI::SetJAREntry(const nsACString &entryPath) { - nsresult rv; - nsCOMPtr serv(do_GetIOService(&rv)); - if (NS_FAILED(rv)) return rv; - mJAREntry.Truncate(); - return serv->ResolveRelativePath(entryPath, NS_LITERAL_CSTRING(""), mJAREntry); + return ::ResolveRelativePath(entryPath, + NS_LITERAL_CSTRING(""), + mJAREntry); } //////////////////////////////////////////////////////////////////////////////// diff --git a/mozilla/netwerk/protocol/res/src/nsResProtocolHandler.cpp b/mozilla/netwerk/protocol/res/src/nsResProtocolHandler.cpp index aafbb98c64a..70ebb32935f 100644 --- a/mozilla/netwerk/protocol/res/src/nsResProtocolHandler.cpp +++ b/mozilla/netwerk/protocol/res/src/nsResProtocolHandler.cpp @@ -91,14 +91,7 @@ nsResURL::GetFile(nsIFile **result) rv = gResHandler->ResolveURI(this, spec); if (NS_FAILED(rv)) return rv; - nsCOMPtr localFile = - do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv); - if (NS_FAILED(rv)) return rv; - - rv = NS_InitFileFromURLSpec(localFile, spec); - if (NS_FAILED(rv)) return rv; - - return CallQueryInterface(localFile, result); + return NS_GetFileFromURLSpec(spec, result, gResHandler->mIOService); } //---------------------------------------------------------------------------- diff --git a/mozilla/netwerk/protocol/res/src/nsResProtocolHandler.h b/mozilla/netwerk/protocol/res/src/nsResProtocolHandler.h index 832732f1d08..ab11a4eede5 100644 --- a/mozilla/netwerk/protocol/res/src/nsResProtocolHandler.h +++ b/mozilla/netwerk/protocol/res/src/nsResProtocolHandler.h @@ -62,6 +62,8 @@ private: nsSupportsHashtable mSubstitutions; nsCOMPtr mIOService; + + friend class nsResURL; }; #endif /* nsResProtocolHandler_h___ */ diff --git a/mozilla/rdf/base/src/nsRDFXMLDataSource.cpp b/mozilla/rdf/base/src/nsRDFXMLDataSource.cpp index 5c48c97f226..cf3559986ec 100644 --- a/mozilla/rdf/base/src/nsRDFXMLDataSource.cpp +++ b/mozilla/rdf/base/src/nsRDFXMLDataSource.cpp @@ -816,17 +816,16 @@ RDFXMLDataSourceImpl::Flush(void) // Is it a file? If so, we can write to it. Some day, it'd be nice // if we didn't care what kind of stream this was... nsCOMPtr fileURL = do_QueryInterface(mURL); + if (fileURL) { nsCOMPtr file; fileURL->GetFile(getter_AddRefs(file)); - if (file) { // if file doesn't exist, create it (void)file->Create(nsIFile::NORMAL_FILE_TYPE, 0666); nsCOMPtr out; - NS_NewLocalFileOutputStream(getter_AddRefs(out), file); - + rv = NS_NewLocalFileOutputStream(getter_AddRefs(out), file); nsCOMPtr bufferedOut; if (out) NS_NewBufferedOutputStream(getter_AddRefs(bufferedOut), out, 4096); diff --git a/mozilla/rdf/datasource/src/nsFileSystemDataSource.cpp b/mozilla/rdf/datasource/src/nsFileSystemDataSource.cpp index 44295531c43..6e0f52d2d63 100644 --- a/mozilla/rdf/datasource/src/nsFileSystemDataSource.cpp +++ b/mozilla/rdf/datasource/src/nsFileSystemDataSource.cpp @@ -246,9 +246,9 @@ FileSystemDataSource::isDirURI(nsIRDFResource* source) rv = source->GetValueConst(&uri); if (NS_FAILED(rv)) return(PR_FALSE); - nsCOMPtr aDir = do_CreateInstance(NS_LOCAL_FILE_CONTRACTID); + nsCOMPtr aDir; - rv = NS_InitFileFromURLSpec(aDir, nsDependentCString(uri)); + rv = NS_GetFileFromURLSpec(nsDependentCString(uri), getter_AddRefs(aDir)); if (NS_FAILED(rv)) return(PR_FALSE); PRBool isDirFlag = PR_FALSE; diff --git a/mozilla/xpfe/components/history/src/nsGlobalHistory.cpp b/mozilla/xpfe/components/history/src/nsGlobalHistory.cpp index 35b4546f671..c86068ac197 100644 --- a/mozilla/xpfe/components/history/src/nsGlobalHistory.cpp +++ b/mozilla/xpfe/components/history/src/nsGlobalHistory.cpp @@ -763,10 +763,11 @@ nsGlobalHistory::AddNewPageToDatabase(const char *aURL, SetRowValue(row, kToken_LastVisitDateColumn, aDate); SetRowValue(row, kToken_FirstVisitDateColumn, aDate); + nsCOMPtr uri; + NS_NewURI(getter_AddRefs(uri), nsDependentCString(aURL), nsnull, nsnull); nsCAutoString hostname; - nsCOMPtr ioService = do_GetService(NS_IOSERVICE_CONTRACTID); - if (!ioService) return NS_ERROR_FAILURE; - ioService->ExtractUrlPart(nsDependentCString(aURL), nsIIOService::url_Host, hostname); + if (uri) + uri->GetHost(hostname); SetRowValue(row, kToken_HostnameColumn, hostname.get()); diff --git a/mozilla/xpfe/components/urlbarhistory/src/nsUrlbarHistory.cpp b/mozilla/xpfe/components/urlbarhistory/src/nsUrlbarHistory.cpp index 299b35c44ae..f39d9cc14f2 100644 --- a/mozilla/xpfe/components/urlbarhistory/src/nsUrlbarHistory.cpp +++ b/mozilla/xpfe/components/urlbarhistory/src/nsUrlbarHistory.cpp @@ -653,18 +653,27 @@ nsUrlbarHistory::VerifyAndCreateEntry(const PRUnichar * aSearchItem, const PRUni searchStrLen = nsCRT::strlen(aSearchItem); nsresult rv; + nsCOMPtr uri; + NS_NewURI(getter_AddRefs(uri), NS_ConvertUCS2toUTF8(aSearchItem)); nsCAutoString filePath; - nsCOMPtr ioService = do_GetService(NS_IOSERVICE_CONTRACTID); - if (!ioService) return NS_ERROR_FAILURE; - ioService->ExtractUrlPart(NS_ConvertUCS2toUTF8(aSearchItem), nsIIOService::url_Path, filePath); - + if (uri) { + nsCOMPtr url(do_QueryInterface(uri)); + if (url) + url->GetFilePath(filePath); + } + // Don't bother checking for hostname if the search string // already has a filepath; if (filePath.Length() > 1) { return NS_OK; } - - ioService->ExtractUrlPart(NS_ConvertUCS2toUTF8(aMatchStr), nsIIOService::url_Path, filePath); + + NS_NewURI(getter_AddRefs(uri), NS_ConvertUCS2toUTF8(aMatchStr)); + if (uri) { + nsCOMPtr url(do_QueryInterface(uri)); + if (url) + url->GetFilePath(filePath); + } // If the match string doesn't have a filepath // we need to do nothing here, return.