1999-07-01 07:52:52 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
*
|
1999-11-06 03:40:37 +00:00
|
|
|
* The contents of this file are subject to the Netscape Public
|
|
|
|
* License Version 1.1 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at http://www.mozilla.org/NPL/
|
1999-07-01 07:52:52 +00:00
|
|
|
*
|
1999-11-06 03:40:37 +00:00
|
|
|
* Software distributed under the License is distributed on an "AS
|
|
|
|
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
|
|
|
* implied. See the License for the specific language governing
|
|
|
|
* rights and limitations under the License.
|
1999-07-01 07:52:52 +00:00
|
|
|
*
|
1999-11-06 03:40:37 +00:00
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Netscape
|
1999-07-01 07:52:52 +00:00
|
|
|
* Communications Corporation. Portions created by Netscape are
|
1999-11-06 03:40:37 +00:00
|
|
|
* Copyright (C) 1998 Netscape Communications Corporation. All
|
|
|
|
* Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
2000-01-11 20:49:15 +00:00
|
|
|
* Pierre Phaneuf <pp@ludusdesign.com>
|
2001-01-31 01:33:03 +00:00
|
|
|
* Gagan Saksena <gagan@netscape.com>
|
1999-07-01 07:52:52 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "nsSimpleURI.h"
|
|
|
|
#include "nscore.h"
|
|
|
|
#include "nsCRT.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "prmem.h"
|
|
|
|
#include "prprf.h"
|
2000-02-03 03:44:22 +00:00
|
|
|
#include "nsURLHelper.h"
|
2001-04-10 06:01:08 +00:00
|
|
|
#include "nsNetCID.h"
|
2001-07-31 19:05:34 +00:00
|
|
|
#include "nsIObjectInputStream.h"
|
|
|
|
#include "nsIObjectOutputStream.h"
|
1999-07-01 07:52:52 +00:00
|
|
|
|
|
|
|
static NS_DEFINE_CID(kSimpleURICID, NS_SIMPLEURI_CID);
|
|
|
|
static NS_DEFINE_CID(kThisSimpleURIImplementationCID,
|
|
|
|
NS_THIS_SIMPLEURI_IMPLEMENTATION_CID);
|
|
|
|
|
|
|
|
static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID);
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// nsSimpleURI methods:
|
|
|
|
|
|
|
|
nsSimpleURI::nsSimpleURI(nsISupports* outer)
|
|
|
|
: mScheme(nsnull),
|
2001-03-13 02:01:07 +00:00
|
|
|
mPath(nsnull)
|
1999-07-01 07:52:52 +00:00
|
|
|
{
|
|
|
|
NS_INIT_AGGREGATED(outer);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsSimpleURI::~nsSimpleURI()
|
|
|
|
{
|
1999-07-28 04:05:10 +00:00
|
|
|
if (mScheme) nsCRT::free(mScheme);
|
|
|
|
if (mPath) nsCRT::free(mPath);
|
1999-07-01 07:52:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_AGGREGATED(nsSimpleURI);
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSimpleURI::AggregatedQueryInterface(const nsIID& aIID, void** aInstancePtr)
|
|
|
|
{
|
1999-08-19 05:36:30 +00:00
|
|
|
NS_ENSURE_ARG_POINTER(aInstancePtr);
|
|
|
|
|
2001-07-31 19:05:34 +00:00
|
|
|
if (aIID.Equals(kISupportsIID)) {
|
|
|
|
*aInstancePtr = GetInner();
|
|
|
|
} else if (aIID.Equals(kThisSimpleURIImplementationCID) || // used by Equals
|
|
|
|
aIID.Equals(NS_GET_IID(nsIURI))) {
|
1999-07-01 07:52:52 +00:00
|
|
|
*aInstancePtr = NS_STATIC_CAST(nsIURI*, this);
|
2001-07-31 19:05:34 +00:00
|
|
|
} else if (aIID.Equals(NS_GET_IID(nsISerializable))) {
|
|
|
|
*aInstancePtr = NS_STATIC_CAST(nsISerializable*, this);
|
|
|
|
} else {
|
|
|
|
*aInstancePtr = nsnull;
|
|
|
|
return NS_NOINTERFACE;
|
|
|
|
}
|
1999-08-19 05:36:30 +00:00
|
|
|
NS_ADDREF((nsISupports*)*aInstancePtr);
|
|
|
|
return NS_OK;
|
1999-07-01 07:52:52 +00:00
|
|
|
}
|
|
|
|
|
2001-07-31 19:05:34 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// nsISerializable methods:
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSimpleURI::Read(nsIObjectInputStream* aStream)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
rv = aStream->ReadStringZ(&mScheme);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
rv = aStream->ReadStringZ(&mPath);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSimpleURI::Write(nsIObjectOutputStream* aStream)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
rv = aStream->WriteStringZ(mScheme);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
rv = aStream->WriteStringZ(mPath);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-07-01 07:52:52 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// nsIURI methods:
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSimpleURI::GetSpec(char* *result)
|
|
|
|
{
|
|
|
|
nsAutoString string;
|
|
|
|
// NS_LOCK_INSTANCE();
|
|
|
|
|
2000-04-01 22:31:00 +00:00
|
|
|
// STRING USE WARNING: perhaps |string| should be |nsCAutoString|? -- scc
|
|
|
|
string.AssignWithConversion(mScheme);
|
|
|
|
string.AppendWithConversion(':');
|
|
|
|
string.AppendWithConversion(mPath);
|
1999-07-01 07:52:52 +00:00
|
|
|
|
|
|
|
// NS_UNLOCK_INSTANCE();
|
|
|
|
*result = string.ToNewCString();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
1999-09-19 23:46:35 +00:00
|
|
|
nsSimpleURI::SetSpec(const char* aSpec)
|
1999-07-01 07:52:52 +00:00
|
|
|
{
|
2000-04-01 22:31:00 +00:00
|
|
|
nsAutoString spec;
|
|
|
|
spec.AssignWithConversion(aSpec);
|
|
|
|
|
1999-09-20 20:57:21 +00:00
|
|
|
PRInt32 pos = spec.Find(":");
|
1999-07-01 07:52:52 +00:00
|
|
|
if (pos == -1)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
nsAutoString scheme;
|
1999-09-20 20:57:21 +00:00
|
|
|
PRInt32 n = spec.Left(scheme, pos);
|
1999-07-01 07:52:52 +00:00
|
|
|
NS_ASSERTION(n == pos, "Left failed");
|
|
|
|
nsAutoString path;
|
1999-09-20 20:57:21 +00:00
|
|
|
PRInt32 count = spec.Length() - pos - 1;
|
1999-07-01 07:52:52 +00:00
|
|
|
n = spec.Mid(path, pos + 1, count);
|
|
|
|
NS_ASSERTION(n == count, "Mid failed");
|
1999-07-31 00:15:49 +00:00
|
|
|
if (mScheme)
|
1999-08-27 12:38:06 +00:00
|
|
|
nsCRT::free(mScheme);
|
1999-07-01 07:52:52 +00:00
|
|
|
mScheme = scheme.ToNewCString();
|
|
|
|
if (mScheme == nsnull)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2001-03-24 00:58:32 +00:00
|
|
|
ToLowerCase(mScheme);
|
1999-07-31 00:15:49 +00:00
|
|
|
if (mPath)
|
1999-08-27 12:38:06 +00:00
|
|
|
nsCRT::free(mPath);
|
1999-07-01 07:52:52 +00:00
|
|
|
mPath = path.ToNewCString();
|
|
|
|
if (mPath == nsnull)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSimpleURI::GetScheme(char* *result)
|
|
|
|
{
|
|
|
|
*result = nsCRT::strdup(mScheme);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
1999-09-19 23:46:35 +00:00
|
|
|
nsSimpleURI::SetScheme(const char* scheme)
|
1999-07-01 07:52:52 +00:00
|
|
|
{
|
|
|
|
if (mScheme) nsCRT::free(mScheme);
|
|
|
|
mScheme = nsCRT::strdup(scheme);
|
2001-03-13 02:01:07 +00:00
|
|
|
ToLowerCase(mScheme);
|
1999-07-01 07:52:52 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2000-05-17 07:12:40 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSimpleURI::GetPrePath(char* *result)
|
|
|
|
{
|
2000-08-19 20:01:04 +00:00
|
|
|
nsCAutoString prePath(mScheme);
|
2000-05-17 07:12:40 +00:00
|
|
|
prePath += ":";
|
|
|
|
*result = prePath.ToNewCString();
|
|
|
|
return *result ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSimpleURI::SetPrePath(const char* scheme)
|
|
|
|
{
|
|
|
|
NS_NOTREACHED("nsSimpleURI::SetPrePath");
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
1999-07-01 07:52:52 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSimpleURI::GetPreHost(char* *result)
|
|
|
|
{
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
1999-09-19 23:46:35 +00:00
|
|
|
nsSimpleURI::SetPreHost(const char* preHost)
|
1999-07-01 07:52:52 +00:00
|
|
|
{
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2000-02-03 03:44:22 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSimpleURI::GetUsername(char* *result)
|
|
|
|
{
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSimpleURI::SetUsername(const char* userName)
|
|
|
|
{
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSimpleURI::GetPassword(char* *result)
|
|
|
|
{
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSimpleURI::SetPassword(const char* password)
|
|
|
|
{
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
1999-07-01 07:52:52 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSimpleURI::GetHost(char* *result)
|
|
|
|
{
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
1999-09-19 23:46:35 +00:00
|
|
|
nsSimpleURI::SetHost(const char* host)
|
1999-07-01 07:52:52 +00:00
|
|
|
{
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSimpleURI::GetPort(PRInt32 *result)
|
|
|
|
{
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSimpleURI::SetPort(PRInt32 port)
|
|
|
|
{
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSimpleURI::GetPath(char* *result)
|
|
|
|
{
|
|
|
|
*result = nsCRT::strdup(mPath);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
1999-09-19 23:46:35 +00:00
|
|
|
nsSimpleURI::SetPath(const char* path)
|
1999-07-01 07:52:52 +00:00
|
|
|
{
|
|
|
|
if (mPath) nsCRT::free(mPath);
|
|
|
|
mPath = nsCRT::strdup(path);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSimpleURI::Equals(nsIURI* other, PRBool *result)
|
|
|
|
{
|
|
|
|
PRBool eq = PR_FALSE;
|
|
|
|
if (other) {
|
|
|
|
// NS_LOCK_INSTANCE();
|
|
|
|
nsSimpleURI* otherUrl;
|
|
|
|
nsresult rv =
|
|
|
|
other->QueryInterface(kThisSimpleURIImplementationCID,
|
|
|
|
(void**)&otherUrl);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
eq = PRBool((0 == PL_strcmp(mScheme, otherUrl->mScheme)) &&
|
|
|
|
(0 == PL_strcmp(mPath, otherUrl->mPath)));
|
|
|
|
NS_RELEASE(otherUrl);
|
|
|
|
}
|
|
|
|
// NS_UNLOCK_INSTANCE();
|
|
|
|
}
|
|
|
|
*result = eq;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2001-01-31 01:33:03 +00:00
|
|
|
NS_IMETHODIMP
|
2001-03-13 02:01:07 +00:00
|
|
|
nsSimpleURI::SchemeIs(const char *i_Scheme, PRBool *o_Equals)
|
2001-01-31 01:33:03 +00:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(o_Equals);
|
2001-03-13 02:01:07 +00:00
|
|
|
if (!i_Scheme) return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
// mScheme is guaranteed to be lower case.
|
|
|
|
if (*i_Scheme == *mScheme || *i_Scheme == (*mScheme - ('a' - 'A')) ) {
|
|
|
|
*o_Equals = PL_strcasecmp(mScheme, i_Scheme) ? PR_FALSE : PR_TRUE;
|
|
|
|
} else {
|
|
|
|
*o_Equals = PR_FALSE;
|
|
|
|
}
|
|
|
|
|
2001-01-31 01:33:03 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-07-01 07:52:52 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSimpleURI::Clone(nsIURI* *result)
|
|
|
|
{
|
|
|
|
nsSimpleURI* url = new nsSimpleURI(nsnull); // XXX outer?
|
|
|
|
if (url == nsnull)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
url->mScheme = nsCRT::strdup(mScheme);
|
|
|
|
if (url->mScheme == nsnull)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
url->mPath = nsCRT::strdup(mPath);
|
|
|
|
if (url->mPath == nsnull)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
*result = url;
|
|
|
|
NS_ADDREF(url);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-11-03 05:13:44 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSimpleURI::Resolve(const char *relativePath, char **result)
|
|
|
|
{
|
2000-02-03 03:44:22 +00:00
|
|
|
return DupString(result,(char*)relativePath);
|
1999-11-03 05:13:44 +00:00
|
|
|
}
|
|
|
|
|
1999-07-01 07:52:52 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
NS_METHOD
|
|
|
|
nsSimpleURI::Create(nsISupports *aOuter, REFNSIID aIID, void **aResult)
|
|
|
|
{
|
1999-08-19 05:36:30 +00:00
|
|
|
NS_ENSURE_ARG_POINTER(aResult);
|
1999-08-27 12:38:06 +00:00
|
|
|
NS_ENSURE_PROPER_AGGREGATION(aOuter, aIID);
|
1999-08-19 05:36:30 +00:00
|
|
|
|
1999-07-01 07:52:52 +00:00
|
|
|
nsSimpleURI* url = new nsSimpleURI(aOuter);
|
|
|
|
if (url == nsnull)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
1999-08-19 05:36:30 +00:00
|
|
|
|
|
|
|
nsresult rv = url->AggregatedQueryInterface(aIID, aResult);
|
|
|
|
|
1999-08-27 12:38:06 +00:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
delete url;
|
1999-07-01 07:52:52 +00:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|