1999-11-08 00:55:12 +03:00
|
|
|
/* -*- Mode: IDL; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Netscape Public License
|
|
|
|
* Version 1.0 (the "NPL"); you may not use this file except in
|
|
|
|
* compliance with the NPL. You may obtain a copy of the NPL at
|
|
|
|
* http://www.mozilla.org/NPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the NPL is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* NPL.
|
|
|
|
*
|
|
|
|
* The Initial Developer of this code under the NPL is Netscape
|
|
|
|
* Communications Corporation. Portions created by Netscape are
|
|
|
|
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
|
|
|
|
* Reserved.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "nsJARURI.h"
|
1999-11-30 07:50:42 +03:00
|
|
|
#include "nsNetUtil.h"
|
1999-11-08 00:55:12 +03:00
|
|
|
#include "nsIIOService.h"
|
2000-01-04 02:31:09 +03:00
|
|
|
#include "nsFileSpec.h"
|
1999-11-08 00:55:12 +03:00
|
|
|
#include "nsCRT.h"
|
|
|
|
#include "nsIComponentManager.h"
|
|
|
|
#include "nsIServiceManager.h"
|
1999-11-12 09:13:13 +03:00
|
|
|
#include "nsIZipReader.h"
|
2000-01-27 11:57:14 +03:00
|
|
|
|
1999-11-08 00:55:12 +03:00
|
|
|
static NS_DEFINE_CID(kIOServiceCID, NS_IOSERVICE_CID);
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
nsJARURI::nsJARURI()
|
2000-01-27 11:57:14 +03:00
|
|
|
: mJAREntry(nsnull)
|
1999-11-08 00:55:12 +03:00
|
|
|
{
|
|
|
|
NS_INIT_REFCNT();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsJARURI::~nsJARURI()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS2(nsJARURI, nsIJARURI, nsIURI)
|
|
|
|
|
|
|
|
NS_METHOD
|
|
|
|
nsJARURI::Create(nsISupports *aOuter, REFNSIID aIID, void **aResult)
|
|
|
|
{
|
|
|
|
if (aOuter)
|
|
|
|
return NS_ERROR_NO_AGGREGATION;
|
|
|
|
|
|
|
|
nsJARURI* uri = new nsJARURI();
|
|
|
|
|
|
|
|
if (uri == nsnull)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
NS_ADDREF(uri);
|
|
|
|
nsresult rv = uri->Init();
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = uri->QueryInterface(aIID, aResult);
|
|
|
|
}
|
|
|
|
NS_RELEASE(uri);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsJARURI::Init()
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define NS_JAR_SCHEME "jar:"
|
|
|
|
#define NS_JAR_DELIMITER "!/"
|
|
|
|
|
2000-01-27 11:57:14 +03:00
|
|
|
nsresult
|
|
|
|
nsJARURI::FormatSpec(const char* entryPath, char* *result)
|
1999-11-08 00:55:12 +03:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
char* jarFileSpec;
|
|
|
|
rv = mJARFile->GetSpec(&jarFileSpec);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
nsCString spec(NS_JAR_SCHEME);
|
|
|
|
spec += jarFileSpec;
|
|
|
|
nsCRT::free(jarFileSpec);
|
|
|
|
spec += NS_JAR_DELIMITER;
|
2000-01-27 11:57:14 +03:00
|
|
|
spec += entryPath;
|
1999-11-08 00:55:12 +03:00
|
|
|
|
2000-01-27 11:57:14 +03:00
|
|
|
*result = nsCRT::strdup(spec);
|
|
|
|
return *result ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// nsURI methods:
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsJARURI::GetSpec(char* *aSpec)
|
|
|
|
{
|
|
|
|
return FormatSpec(mJAREntry, aSpec);
|
1999-11-08 00:55:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsJARURI::SetSpec(const char * aSpec)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
NS_WITH_SERVICE(nsIIOService, serv, kIOServiceCID, &rv);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
PRUint32 startPos, endPos;
|
|
|
|
rv = serv->ExtractScheme(aSpec, &startPos, &endPos, nsnull);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
if (nsCRT::strncmp("jar", &aSpec[startPos], endPos - startPos - 1) != 0)
|
2000-01-27 11:57:14 +03:00
|
|
|
return NS_ERROR_MALFORMED_URI;
|
1999-11-08 00:55:12 +03:00
|
|
|
|
|
|
|
// Search backward from the end for the "!/" delimiter. Remember, jar URLs
|
|
|
|
// can nest, e.g.:
|
|
|
|
// jar:jar:http://www.foo.com/bar.jar!/a.jar!/b.html
|
|
|
|
// This gets the b.html document from out of the a.jar file, that's
|
|
|
|
// contained within the bar.jar file.
|
|
|
|
|
|
|
|
nsCAutoString jarPath(aSpec);
|
|
|
|
PRInt32 pos = jarPath.RFind(NS_JAR_DELIMITER);
|
|
|
|
if (pos == -1 || endPos + 1 > (PRUint32)pos)
|
|
|
|
return NS_ERROR_MALFORMED_URI;
|
|
|
|
|
|
|
|
jarPath.Cut(pos, jarPath.Length());
|
|
|
|
jarPath.Cut(0, endPos);
|
|
|
|
|
|
|
|
rv = serv->NewURI(jarPath, nsnull, getter_AddRefs(mJARFile));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
nsCAutoString entry(aSpec);
|
|
|
|
entry.Cut(0, pos + 2); // 2 == strlen(NS_JAR_DELIMITER)
|
|
|
|
|
2000-01-27 11:57:14 +03:00
|
|
|
rv = serv->ResolveRelativePath(entry, nsnull, &mJAREntry);
|
|
|
|
return rv;
|
1999-11-08 00:55:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsJARURI::GetScheme(char * *aScheme)
|
|
|
|
{
|
|
|
|
*aScheme = nsCRT::strdup("jar");
|
|
|
|
return *aScheme ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsJARURI::SetScheme(const char * aScheme)
|
|
|
|
{
|
2000-01-27 11:57:14 +03:00
|
|
|
// doesn't make sense to set the scheme of a jar: URL
|
|
|
|
return NS_ERROR_FAILURE;
|
1999-11-08 00:55:12 +03:00
|
|
|
}
|
|
|
|
|
2000-02-03 06:44:22 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsJARURI::GetUsername(char * *aUsername)
|
|
|
|
{
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsJARURI::SetUsername(const char * aUsername)
|
|
|
|
{
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsJARURI::GetPassword(char * *aPassword)
|
|
|
|
{
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsJARURI::SetPassword(const char * aPassword)
|
|
|
|
{
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
1999-11-08 00:55:12 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsJARURI::GetPreHost(char * *aPreHost)
|
|
|
|
{
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsJARURI::SetPreHost(const char * aPreHost)
|
|
|
|
{
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsJARURI::GetHost(char * *aHost)
|
|
|
|
{
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsJARURI::SetHost(const char * aHost)
|
|
|
|
{
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsJARURI::GetPort(PRInt32 *aPort)
|
|
|
|
{
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsJARURI::SetPort(PRInt32 aPort)
|
|
|
|
{
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsJARURI::GetPath(char * *aPath)
|
|
|
|
{
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsJARURI::SetPath(const char * aPath)
|
|
|
|
{
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2000-02-03 06:44:22 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsJARURI::GetURLParser(nsIURLParser * *aURLParser)
|
|
|
|
{
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsJARURI::SetURLParser(nsIURLParser * aURLParser)
|
|
|
|
{
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
1999-11-08 00:55:12 +03:00
|
|
|
NS_IMETHODIMP
|
2000-01-27 11:57:14 +03:00
|
|
|
nsJARURI::Equals(nsIURI *other, PRBool *result)
|
1999-11-08 00:55:12 +03:00
|
|
|
{
|
2000-01-27 11:57:14 +03:00
|
|
|
nsresult rv;
|
|
|
|
*result = PR_FALSE;
|
|
|
|
|
|
|
|
nsJARURI* otherJAR;
|
|
|
|
rv = other->QueryInterface(NS_GET_IID(nsIJARURI), (void**)&otherJAR);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return NS_OK; // not equal
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> otherJARFile;
|
|
|
|
rv = otherJAR->GetJARFile(getter_AddRefs(otherJARFile));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
PRBool equal;
|
|
|
|
rv = mJARFile->Equals(otherJARFile, &equal);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
if (!equal)
|
|
|
|
return NS_OK; // not equal
|
|
|
|
|
|
|
|
char* otherJAREntry;
|
|
|
|
rv = otherJAR->GetJAREntry(&otherJAREntry);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
*result = nsCRT::strcmp(mJAREntry, otherJAREntry) == 0;
|
|
|
|
nsCRT::free(otherJAREntry);
|
|
|
|
return NS_OK;
|
1999-11-08 00:55:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2000-01-27 11:57:14 +03:00
|
|
|
nsJARURI::Clone(nsIURI **result)
|
1999-11-08 00:55:12 +03:00
|
|
|
{
|
2000-01-27 11:57:14 +03:00
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> newJARFile;
|
|
|
|
rv = mJARFile->Clone(getter_AddRefs(newJARFile));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
char* newJAREntry = nsCRT::strdup(mJAREntry);
|
|
|
|
if (newJAREntry == nsnull)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
nsJARURI* uri = new nsJARURI();
|
|
|
|
if (uri == nsnull)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
NS_ADDREF(uri);
|
|
|
|
uri->mJARFile = newJARFile;
|
|
|
|
uri->mJAREntry = newJAREntry;
|
|
|
|
*result = uri;
|
|
|
|
|
|
|
|
return NS_OK;
|
1999-11-08 00:55:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2000-01-27 11:57:14 +03:00
|
|
|
nsJARURI::SetRelativePath(const char *relativePath)
|
1999-11-08 00:55:12 +03:00
|
|
|
{
|
2000-01-27 11:57:14 +03:00
|
|
|
nsresult rv;
|
|
|
|
NS_WITH_SERVICE(nsIIOService, serv, kIOServiceCID, &rv);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
nsCAutoString path(mJAREntry);
|
|
|
|
PRInt32 pos = path.RFind("/");
|
|
|
|
if (pos >= 0)
|
|
|
|
path.Truncate(pos + 1);
|
|
|
|
else
|
|
|
|
path = "";
|
|
|
|
|
|
|
|
char* resolvedEntry;
|
|
|
|
rv = serv->ResolveRelativePath(relativePath, path.GetBuffer(),
|
|
|
|
&resolvedEntry);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
nsCRT::free(mJAREntry);
|
|
|
|
mJAREntry = resolvedEntry;
|
|
|
|
return NS_OK;
|
1999-11-08 00:55:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2000-01-27 11:57:14 +03:00
|
|
|
nsJARURI::Resolve(const char *relativePath, char **result)
|
1999-11-08 00:55:12 +03:00
|
|
|
{
|
2000-01-27 11:57:14 +03:00
|
|
|
nsresult rv;
|
|
|
|
NS_WITH_SERVICE(nsIIOService, serv, kIOServiceCID, &rv);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
nsCAutoString path(mJAREntry);
|
|
|
|
PRInt32 pos = path.RFind("/");
|
|
|
|
if (pos >= 0)
|
|
|
|
path.Truncate(pos + 1);
|
|
|
|
else
|
|
|
|
path = "";
|
|
|
|
|
|
|
|
char* resolvedEntry;
|
|
|
|
rv = serv->ResolveRelativePath(relativePath, path.GetBuffer(),
|
|
|
|
&resolvedEntry);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
rv = FormatSpec(resolvedEntry, result);
|
|
|
|
nsCRT::free(resolvedEntry);
|
|
|
|
return rv;
|
1999-11-08 00:55:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// nsIJARUri methods:
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2000-01-27 11:57:14 +03:00
|
|
|
nsJARURI::GetJARFile(nsIURI* *jarFile)
|
1999-11-08 00:55:12 +03:00
|
|
|
{
|
2000-01-27 11:57:14 +03:00
|
|
|
*jarFile = mJARFile;
|
|
|
|
NS_ADDREF(*jarFile);
|
1999-11-08 00:55:12 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2000-01-27 11:57:14 +03:00
|
|
|
nsJARURI::SetJARFile(nsIURI* jarFile)
|
1999-11-08 00:55:12 +03:00
|
|
|
{
|
2000-01-27 11:57:14 +03:00
|
|
|
mJARFile = jarFile;
|
1999-11-08 00:55:12 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2000-01-27 11:57:14 +03:00
|
|
|
nsJARURI::GetJAREntry(char* *entryPath)
|
1999-11-08 00:55:12 +03:00
|
|
|
{
|
2000-01-27 11:57:14 +03:00
|
|
|
nsCAutoString entry(mJAREntry);
|
|
|
|
PRInt32 pos = entry.RFindCharInSet("#?;");
|
|
|
|
if (pos >= 0)
|
|
|
|
entry.Truncate(pos);
|
|
|
|
*entryPath = entry.ToNewCString();
|
|
|
|
return *entryPath ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
|
1999-11-08 00:55:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2000-01-27 11:57:14 +03:00
|
|
|
nsJARURI::SetJAREntry(const char* entryPath)
|
1999-11-08 00:55:12 +03:00
|
|
|
{
|
2000-01-27 11:57:14 +03:00
|
|
|
nsresult rv;
|
|
|
|
NS_WITH_SERVICE(nsIIOService, serv, kIOServiceCID, &rv);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
1999-11-08 00:55:12 +03:00
|
|
|
if (mJAREntry)
|
|
|
|
nsCRT::free(mJAREntry);
|
2000-01-27 11:57:14 +03:00
|
|
|
|
|
|
|
rv = serv->ResolveRelativePath(entryPath, nsnull, &mJAREntry);
|
|
|
|
return rv;
|
1999-11-08 00:55:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|