1999-08-23 03:48:53 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
*
|
1999-11-06 03:43:54 +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-08-23 03:48:53 +00:00
|
|
|
*
|
1999-11-06 03:43:54 +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-08-23 03:48:53 +00:00
|
|
|
*
|
|
|
|
* The Original Code is Mozilla Communicator client code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Netscape Communications
|
1999-11-06 03:43:54 +00:00
|
|
|
* Corporation. Portions created by Netscape are
|
|
|
|
* Copyright (C) 1998 Netscape Communications Corporation. All
|
|
|
|
* Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
2000-01-11 20:49:15 +00:00
|
|
|
* Pierre Phaneuf <pp@ludusdesign.com>
|
1999-08-23 03:48:53 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
A directory viewer object. Parses "application/http-index-format"
|
|
|
|
per Lou Montulli's original spec:
|
|
|
|
|
|
|
|
http://www.area.com/~roeber/file_format.html
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "jsapi.h"
|
|
|
|
#include "nsCOMPtr.h"
|
2000-01-24 21:28:28 +00:00
|
|
|
#include "nsCRT.h"
|
1999-08-24 04:16:33 +00:00
|
|
|
#include "nsEscape.h"
|
1999-11-30 04:50:42 +00:00
|
|
|
#include "nsNetUtil.h"
|
1999-08-23 03:48:53 +00:00
|
|
|
#include "nsIContentViewer.h"
|
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "nsIDocumentLoader.h"
|
2000-01-29 06:09:13 +00:00
|
|
|
#include "nsIDocumentLoaderFactory.h"
|
1999-08-23 03:48:53 +00:00
|
|
|
#include "nsIDocumentViewer.h"
|
|
|
|
#include "nsIEnumerator.h"
|
|
|
|
#include "nsIGenericFactory.h"
|
|
|
|
#include "nsIHTTPIndex.h"
|
|
|
|
#include "nsIRDFContainer.h"
|
|
|
|
#include "nsIRDFContainerUtils.h"
|
|
|
|
#include "nsIRDFDataSource.h"
|
|
|
|
#include "nsIRDFService.h"
|
|
|
|
#include "nsIScriptContext.h"
|
1999-12-03 09:24:22 +00:00
|
|
|
#include "nsIScriptGlobalObject.h"
|
1999-08-23 03:48:53 +00:00
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsISupportsArray.h"
|
|
|
|
#include "nsIXPConnect.h"
|
|
|
|
#include "nsEnumeratorUtils.h"
|
|
|
|
#include "nsRDFCID.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsVoidArray.h"
|
|
|
|
#include "nsXPIDLString.h"
|
|
|
|
#include "rdf.h"
|
1999-11-13 07:36:28 +00:00
|
|
|
#include "nsIInterfaceRequestor.h"
|
2000-02-03 03:44:22 +00:00
|
|
|
#include "iostream.h"
|
|
|
|
#include "nsIIOService.h"
|
1999-08-23 03:48:53 +00:00
|
|
|
|
2000-02-08 01:17:27 +00:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
//
|
1999-08-23 03:48:53 +00:00
|
|
|
// Common CIDs
|
2000-02-08 01:17:27 +00:00
|
|
|
//
|
1999-08-23 03:48:53 +00:00
|
|
|
|
|
|
|
// {82776710-5690-11d3-BE36-00104BDE6048}
|
|
|
|
#define NS_DIRECTORYVIEWERFACTORY_CID \
|
|
|
|
{ 0x82776710, 0x5690, 0x11d3, { 0xbe, 0x36, 0x0, 0x10, 0x4b, 0xde, 0x60, 0x48 } }
|
|
|
|
|
2000-02-03 03:44:22 +00:00
|
|
|
static NS_DEFINE_CID(kIOServiceCID, NS_IOSERVICE_CID);
|
1999-08-23 03:48:53 +00:00
|
|
|
|
|
|
|
#define HTTPINDEX_NAMESPACE_URI "urn:http-index-format:"
|
|
|
|
|
2000-02-08 01:17:27 +00:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
//
|
1999-08-23 03:48:53 +00:00
|
|
|
// nsHTTPIndex
|
2000-02-08 01:17:27 +00:00
|
|
|
//
|
1999-08-23 03:48:53 +00:00
|
|
|
|
|
|
|
class nsHTTPIndex : public nsIHTTPIndex
|
|
|
|
{
|
1999-08-23 04:19:48 +00:00
|
|
|
protected:
|
1999-08-23 03:48:53 +00:00
|
|
|
// We grab a reference to the content viewer container (which
|
|
|
|
// indirectly owns us) so that we can insert ourselves as a global
|
|
|
|
// in the script context _after_ the XUL doc has been embedded into
|
|
|
|
// content viewer. We'll know that this has happened once we receive
|
|
|
|
// an OnStartRequest() notification
|
1999-11-13 07:36:28 +00:00
|
|
|
nsISupports* mContainer; // [WEAK]
|
1999-08-23 03:48:53 +00:00
|
|
|
|
1999-08-25 01:32:40 +00:00
|
|
|
nsCString mBaseURL;
|
1999-08-23 03:48:53 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIRDFDataSource> mDataSource;
|
|
|
|
|
1999-11-13 07:36:28 +00:00
|
|
|
nsHTTPIndex(nsISupports* aContainer);
|
1999-08-23 03:48:53 +00:00
|
|
|
nsresult Init(nsIURI* aBaseURL);
|
1999-08-23 04:19:48 +00:00
|
|
|
virtual ~nsHTTPIndex();
|
1999-08-23 03:48:53 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
1999-11-13 07:36:28 +00:00
|
|
|
static nsresult Create(nsIURI* aBaseURI, nsISupports* aContainer, nsIHTTPIndex** aResult);
|
1999-08-23 03:48:53 +00:00
|
|
|
|
|
|
|
// nsISupports interface
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
// nsIHTTPIndex interface
|
|
|
|
NS_IMETHOD GetBaseURL(char** _result);
|
|
|
|
NS_IMETHOD GetDataSource(nsIRDFDataSource** _result);
|
|
|
|
NS_IMETHOD CreateListener(nsIStreamListener** _result);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2000-02-08 01:17:27 +00:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
//
|
1999-08-23 03:48:53 +00:00
|
|
|
// nsHTTPIndexParser
|
|
|
|
//
|
|
|
|
|
|
|
|
class nsHTTPIndexParser : public nsIStreamListener
|
|
|
|
{
|
1999-08-23 04:19:48 +00:00
|
|
|
protected:
|
1999-08-23 03:48:53 +00:00
|
|
|
static nsrefcnt gRefCnt;
|
|
|
|
static nsIRDFService* gRDF;
|
|
|
|
static nsIRDFResource* kHTTPIndex_Comment;
|
|
|
|
static nsIRDFResource* kHTTPIndex_Filename;
|
1999-09-21 21:43:41 +00:00
|
|
|
static nsIRDFResource* kHTTPIndex_Loading;
|
|
|
|
static nsIRDFLiteral* kTrueLiteral;
|
1999-08-23 03:48:53 +00:00
|
|
|
|
1999-11-10 23:56:29 +00:00
|
|
|
static nsresult ParseLiteral(nsIRDFResource *arc, nsString& aValue, nsIRDFNode** aResult);
|
|
|
|
static nsresult ParseDate(nsIRDFResource *arc, nsString& aValue, nsIRDFNode** aResult);
|
|
|
|
static nsresult ParseInt(nsIRDFResource *arc, nsString& aValue, nsIRDFNode** aResult);
|
1999-08-26 05:54:04 +00:00
|
|
|
|
1999-08-23 03:48:53 +00:00
|
|
|
struct Field {
|
|
|
|
const char* mName;
|
1999-11-10 23:56:29 +00:00
|
|
|
nsresult (*mParse)(nsIRDFResource *arc, nsString& aValue, nsIRDFNode** aResult);
|
1999-08-26 05:54:04 +00:00
|
|
|
nsIRDFResource* mProperty;
|
1999-08-23 03:48:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static Field gFieldTable[];
|
|
|
|
|
|
|
|
nsHTTPIndex* mHTTPIndex; // [WEAK]
|
|
|
|
|
|
|
|
nsCOMPtr<nsIRDFDataSource> mDataSource;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> mDirectoryURI;
|
|
|
|
nsCOMPtr<nsIRDFResource> mDirectory;
|
|
|
|
nsCOMPtr<nsIRDFContainer> mDirectoryContainer;
|
|
|
|
|
2000-02-08 01:17:27 +00:00
|
|
|
nsCString mBuf;
|
1999-11-10 23:51:10 +00:00
|
|
|
nsresult ProcessData();
|
1999-08-23 03:48:53 +00:00
|
|
|
nsresult ParseFormat(const char* aFormatStr);
|
|
|
|
nsresult ParseData(const char* aDataStr);
|
|
|
|
|
|
|
|
nsAutoString mComment;
|
|
|
|
|
|
|
|
nsVoidArray mFormat;
|
|
|
|
|
1999-11-13 07:36:28 +00:00
|
|
|
nsHTTPIndexParser(nsHTTPIndex* aHTTPIndex, nsISupports* aContainer);
|
1999-08-23 03:48:53 +00:00
|
|
|
nsresult Init();
|
|
|
|
|
|
|
|
virtual ~nsHTTPIndexParser();
|
|
|
|
|
|
|
|
// If this is set, then we need to bind the nsIHTTPIndex object to
|
|
|
|
// the global object when we get an OnStartRequest() notification
|
|
|
|
// (at this point, we'll know that the XUL document has been
|
|
|
|
// embedded and the global object won't get clobbered.
|
1999-11-13 07:36:28 +00:00
|
|
|
nsISupports* mContainer;
|
1999-08-23 03:48:53 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
static nsresult Create(nsHTTPIndex* aHTTPIndex,
|
1999-11-13 07:36:28 +00:00
|
|
|
nsISupports* aContainer,
|
1999-08-23 03:48:53 +00:00
|
|
|
nsIStreamListener** aResult);
|
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
1999-09-16 01:16:22 +00:00
|
|
|
NS_DECL_NSISTREAMOBSERVER
|
|
|
|
NS_DECL_NSISTREAMLISTENER
|
1999-08-23 03:48:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
nsrefcnt nsHTTPIndexParser::gRefCnt = 0;
|
|
|
|
nsIRDFService* nsHTTPIndexParser::gRDF;
|
|
|
|
nsIRDFResource* nsHTTPIndexParser::kHTTPIndex_Comment;
|
|
|
|
nsIRDFResource* nsHTTPIndexParser::kHTTPIndex_Filename;
|
1999-09-21 21:43:41 +00:00
|
|
|
nsIRDFResource* nsHTTPIndexParser::kHTTPIndex_Loading;
|
|
|
|
nsIRDFLiteral* nsHTTPIndexParser::kTrueLiteral;
|
1999-08-23 03:48:53 +00:00
|
|
|
|
2000-02-08 01:17:27 +00:00
|
|
|
// This table tells us how to parse the fields in the HTTP-index
|
|
|
|
// stream into an RDF graph.
|
1999-08-23 03:48:53 +00:00
|
|
|
nsHTTPIndexParser::Field
|
|
|
|
nsHTTPIndexParser::gFieldTable[] = {
|
1999-08-26 05:54:04 +00:00
|
|
|
{ "Filename", nsHTTPIndexParser::ParseLiteral, nsnull },
|
|
|
|
{ "Content-Length", nsHTTPIndexParser::ParseInt, nsnull },
|
|
|
|
{ "Last-Modified", nsHTTPIndexParser::ParseDate, nsnull },
|
|
|
|
{ "Content-Type", nsHTTPIndexParser::ParseLiteral, nsnull },
|
|
|
|
{ "File-Type", nsHTTPIndexParser::ParseLiteral, nsnull },
|
|
|
|
{ "Permissions", nsHTTPIndexParser::ParseLiteral, nsnull },
|
|
|
|
{ nsnull, nsHTTPIndexParser::ParseLiteral, nsnull },
|
1999-08-23 03:48:53 +00:00
|
|
|
};
|
|
|
|
|
1999-11-13 07:36:28 +00:00
|
|
|
nsHTTPIndexParser::nsHTTPIndexParser(nsHTTPIndex* aHTTPIndex, nsISupports* aContainer)
|
1999-08-23 03:48:53 +00:00
|
|
|
: mHTTPIndex(aHTTPIndex),
|
|
|
|
mContainer(aContainer)
|
|
|
|
{
|
|
|
|
NS_INIT_REFCNT();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-10 23:51:10 +00:00
|
|
|
|
1999-08-23 03:48:53 +00:00
|
|
|
nsresult
|
|
|
|
nsHTTPIndexParser::Init()
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(mHTTPIndex != nsnull, "not initialized");
|
|
|
|
if (! mHTTPIndex)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
rv = mHTTPIndex->GetDataSource(getter_AddRefs(mDataSource));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
1999-09-21 01:41:29 +00:00
|
|
|
// No datasource. Uh oh. We won't be much use then.
|
|
|
|
if (! mDataSource)
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
|
1999-08-23 03:48:53 +00:00
|
|
|
if (gRefCnt++ == 0) {
|
|
|
|
rv = nsServiceManager::GetService("component://netscape/rdf/rdf-service",
|
2000-01-11 20:49:15 +00:00
|
|
|
NS_GET_IID(nsIRDFService),
|
1999-08-23 03:48:53 +00:00
|
|
|
NS_REINTERPRET_CAST(nsISupports**, &gRDF));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
rv = gRDF->GetResource(HTTPINDEX_NAMESPACE_URI "Comment", &kHTTPIndex_Comment);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
rv = gRDF->GetResource(HTTPINDEX_NAMESPACE_URI "Filename", &kHTTPIndex_Filename);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
1999-09-21 21:43:41 +00:00
|
|
|
rv = gRDF->GetResource(HTTPINDEX_NAMESPACE_URI "Loading", &kHTTPIndex_Loading);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
rv = gRDF->GetLiteral(nsAutoString("true").GetUnicode(), &kTrueLiteral);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
1999-08-23 03:48:53 +00:00
|
|
|
for (Field* field = gFieldTable; field->mName; ++field) {
|
1999-11-10 23:51:10 +00:00
|
|
|
nsCAutoString str(HTTPINDEX_NAMESPACE_URI);
|
1999-08-23 03:48:53 +00:00
|
|
|
str += field->mName;
|
|
|
|
|
1999-08-26 05:54:04 +00:00
|
|
|
rv = gRDF->GetResource(str, &field->mProperty);
|
1999-08-23 03:48:53 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-11-10 23:51:10 +00:00
|
|
|
|
|
|
|
|
1999-08-23 03:48:53 +00:00
|
|
|
nsHTTPIndexParser::~nsHTTPIndexParser()
|
|
|
|
{
|
|
|
|
if (--gRefCnt == 0) {
|
|
|
|
NS_IF_RELEASE(kHTTPIndex_Comment);
|
|
|
|
NS_IF_RELEASE(kHTTPIndex_Filename);
|
1999-09-21 21:43:41 +00:00
|
|
|
NS_IF_RELEASE(kHTTPIndex_Loading);
|
|
|
|
NS_IF_RELEASE(kTrueLiteral);
|
1999-08-23 03:48:53 +00:00
|
|
|
|
|
|
|
for (Field* field = gFieldTable; field->mName; ++field) {
|
1999-08-26 05:54:04 +00:00
|
|
|
NS_IF_RELEASE(field->mProperty);
|
1999-08-23 03:48:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsServiceManager::ReleaseService("component://netscape/rdf/rdf-service", gRDF);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-10 23:51:10 +00:00
|
|
|
|
1999-08-23 03:48:53 +00:00
|
|
|
nsresult
|
|
|
|
nsHTTPIndexParser::Create(nsHTTPIndex* aHTTPIndex,
|
1999-11-13 07:36:28 +00:00
|
|
|
nsISupports* aContainer,
|
1999-08-23 03:48:53 +00:00
|
|
|
nsIStreamListener** aResult)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(aHTTPIndex != nsnull, "null ptr");
|
|
|
|
if (! aHTTPIndex)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
nsHTTPIndexParser* result = new nsHTTPIndexParser(aHTTPIndex, aContainer);
|
|
|
|
if (! result)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
nsresult rv = result->Init();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
delete result;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ADDREF(result);
|
|
|
|
*aResult = result;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2000-03-05 21:26:01 +00:00
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS2(nsHTTPIndexParser, nsIStreamListener, nsIStreamObserver);
|
1999-08-23 03:48:53 +00:00
|
|
|
|
1999-11-10 23:51:10 +00:00
|
|
|
|
1999-08-23 03:48:53 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTTPIndexParser::OnStartRequest(nsIChannel* aChannel, nsISupports* aContext)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
1999-12-06 23:45:34 +00:00
|
|
|
// This should only run once...
|
|
|
|
if (mContainer) {
|
|
|
|
// We need to undo the AddRef() on the nsHTTPIndex object that
|
|
|
|
// happened in nsDirectoryViewerFactory::CreateInstance(). We'll
|
|
|
|
// stuff it into an nsCOMPtr (because we _know_ it'll get release
|
|
|
|
// if any errors occur)...
|
|
|
|
nsCOMPtr<nsIHTTPIndex> httpindex = do_QueryInterface(mHTTPIndex);
|
|
|
|
|
|
|
|
// ...and then _force_ a release here
|
|
|
|
mHTTPIndex->Release();
|
|
|
|
|
|
|
|
// Now get the content viewer container's script object.
|
|
|
|
nsCOMPtr<nsIScriptGlobalObject> scriptGlobal(do_GetInterface(mContainer));
|
|
|
|
NS_ENSURE_TRUE(scriptGlobal, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIScriptContext> context;
|
|
|
|
rv = scriptGlobal->GetContext(getter_AddRefs(context));
|
|
|
|
NS_ENSURE_TRUE(context, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
JSContext* jscontext = NS_REINTERPRET_CAST(JSContext*, context->GetNativeContext());
|
1999-12-18 20:29:29 +00:00
|
|
|
JSObject* global = JS_GetGlobalObject(jscontext);
|
1999-12-06 23:45:34 +00:00
|
|
|
|
|
|
|
// Using XPConnect, wrap the HTTP index object...
|
|
|
|
static NS_DEFINE_CID(kXPConnectCID, NS_XPCONNECT_CID);
|
|
|
|
NS_WITH_SERVICE(nsIXPConnect, xpc, kXPConnectCID, &rv);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
1999-08-23 03:48:53 +00:00
|
|
|
|
2000-01-26 08:38:10 +00:00
|
|
|
nsCOMPtr<nsIXPConnectJSObjectHolder> wrapper;
|
1999-12-06 23:45:34 +00:00
|
|
|
rv = xpc->WrapNative(jscontext,
|
1999-12-18 20:29:29 +00:00
|
|
|
global,
|
1999-12-06 23:45:34 +00:00
|
|
|
httpindex,
|
2000-01-11 20:49:15 +00:00
|
|
|
NS_GET_IID(nsIHTTPIndex),
|
1999-12-06 23:45:34 +00:00
|
|
|
getter_AddRefs(wrapper));
|
1999-08-23 03:48:53 +00:00
|
|
|
|
1999-12-06 23:45:34 +00:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "unable to xpconnect-wrap http-index");
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
1999-08-23 03:48:53 +00:00
|
|
|
|
1999-12-06 23:45:34 +00:00
|
|
|
JSObject* jsobj;
|
|
|
|
rv = wrapper->GetJSObject(&jsobj);
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "unable to get jsobj from xpconnect wrapper");
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
1999-08-23 03:48:53 +00:00
|
|
|
|
1999-12-06 23:45:34 +00:00
|
|
|
jsval jslistener = OBJECT_TO_JSVAL(jsobj);
|
1999-08-23 03:48:53 +00:00
|
|
|
|
1999-12-06 23:45:34 +00:00
|
|
|
// ...and stuff it into the global context
|
|
|
|
PRBool ok;
|
|
|
|
ok = JS_SetProperty(jscontext, global, "HTTPIndex", &jslistener);
|
1999-08-23 03:48:53 +00:00
|
|
|
|
1999-12-06 23:45:34 +00:00
|
|
|
NS_ASSERTION(ok, "unable to set Listener property");
|
|
|
|
if (! ok)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
1999-08-23 03:48:53 +00:00
|
|
|
|
|
|
|
// Save off some information about the stream we're about to parse.
|
|
|
|
rv = aChannel->GetURI(getter_AddRefs(mDirectoryURI));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
nsXPIDLCString uristr;
|
|
|
|
rv = mDirectoryURI->GetSpec(getter_Copies(uristr));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
rv = gRDF->GetResource(uristr, getter_AddRefs(mDirectory));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
NS_WITH_SERVICE(nsIRDFContainerUtils, rdfc, "component://netscape/rdf/container-utils", &rv);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
rv = rdfc->MakeSeq(mDataSource, mDirectory, getter_AddRefs(mDirectoryContainer));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
1999-09-21 21:43:41 +00:00
|
|
|
// Mark the directory as "loading"
|
|
|
|
rv = mDataSource->Assert(mDirectory, kHTTPIndex_Loading, kTrueLiteral, PR_TRUE);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
1999-08-23 03:48:53 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-11-10 23:51:10 +00:00
|
|
|
|
|
|
|
|
1999-08-23 03:48:53 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTTPIndexParser::OnStopRequest(nsIChannel* aChannel,
|
|
|
|
nsISupports* aContext,
|
|
|
|
nsresult aStatus,
|
|
|
|
const PRUnichar* aErrorMsg)
|
|
|
|
{
|
1999-09-21 01:41:29 +00:00
|
|
|
// If mDirectory isn't set, then we should just bail. Either an
|
|
|
|
// error occurred and OnStartRequest() never got called, or
|
|
|
|
// something exploded in OnStartRequest().
|
|
|
|
if (! mDirectory)
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
// XXX Should we do anything different if aStatus != NS_OK?
|
|
|
|
|
2000-02-08 01:17:27 +00:00
|
|
|
// Clean up any remaining data
|
|
|
|
if (mBuf.Length() > 0)
|
1999-11-10 23:51:10 +00:00
|
|
|
ProcessData();
|
1999-08-23 03:48:53 +00:00
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIRDFLiteral> comment;
|
|
|
|
rv = gRDF->GetLiteral(mComment.GetUnicode(), getter_AddRefs(comment));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
rv = mDataSource->Assert(mDirectory, kHTTPIndex_Comment, comment, PR_TRUE);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
1999-09-21 21:43:41 +00:00
|
|
|
// Remove the 'loading' annotation.
|
|
|
|
rv = mDataSource->Unassert(mDirectory, kHTTPIndex_Loading, kTrueLiteral);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
1999-08-23 03:48:53 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTTPIndexParser::OnDataAvailable(nsIChannel* aChannel,
|
|
|
|
nsISupports* aContext,
|
|
|
|
nsIInputStream* aStream,
|
|
|
|
PRUint32 aSourceOffset,
|
|
|
|
PRUint32 aCount)
|
|
|
|
{
|
2000-02-08 01:17:27 +00:00
|
|
|
// Make sure there's some data to process...
|
|
|
|
if (aCount < 1)
|
|
|
|
return NS_OK;
|
1999-11-10 23:51:10 +00:00
|
|
|
|
2000-02-08 01:17:27 +00:00
|
|
|
PRInt32 len = mBuf.Length();
|
1999-11-10 23:51:10 +00:00
|
|
|
|
2000-02-08 01:17:27 +00:00
|
|
|
// Ensure that our mBuf has capacity to hold the data we're about to
|
|
|
|
// read.
|
|
|
|
mBuf.SetCapacity(len + aCount + 1);
|
|
|
|
if (! mBuf.mStr)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
1999-11-10 23:51:10 +00:00
|
|
|
|
2000-02-08 01:17:27 +00:00
|
|
|
// Now read the data into our buffer.
|
|
|
|
nsresult rv;
|
|
|
|
PRUint32 count;
|
|
|
|
rv = aStream->Read(mBuf.mStr + len, aCount, &count);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
1999-11-10 23:51:10 +00:00
|
|
|
|
2000-02-08 01:17:27 +00:00
|
|
|
// Set the string's length according to the amount of data we've read.
|
|
|
|
//
|
|
|
|
// XXX You'd think that mBuf.SetLength() would do this, but it
|
|
|
|
// doesn't. It calls Truncate(), so you can't set the length to
|
|
|
|
// something longer.
|
|
|
|
mBuf.mLength = len + count;
|
|
|
|
AddNullTerminator(mBuf);
|
1999-11-10 23:51:10 +00:00
|
|
|
|
2000-02-08 01:17:27 +00:00
|
|
|
return ProcessData();
|
1999-08-23 03:48:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-10 23:51:10 +00:00
|
|
|
|
1999-08-23 03:48:53 +00:00
|
|
|
nsresult
|
1999-11-10 23:51:10 +00:00
|
|
|
nsHTTPIndexParser::ProcessData()
|
1999-08-23 03:48:53 +00:00
|
|
|
{
|
1999-11-10 23:51:10 +00:00
|
|
|
while(PR_TRUE)
|
|
|
|
{
|
2000-02-08 01:17:27 +00:00
|
|
|
PRInt32 eol = mBuf.FindCharInSet("\n\r");
|
1999-11-10 23:51:10 +00:00
|
|
|
if (eol < 0) break;
|
|
|
|
|
2000-02-08 01:17:27 +00:00
|
|
|
nsCAutoString line;
|
|
|
|
mBuf.Left(line, eol);
|
|
|
|
mBuf.Cut(0, eol + 1);
|
1999-11-10 23:51:10 +00:00
|
|
|
|
2000-02-08 01:17:27 +00:00
|
|
|
if (line.Length() >= 4)
|
1999-11-10 23:51:10 +00:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2000-02-08 01:17:27 +00:00
|
|
|
const char *buf = line;
|
1999-11-10 23:51:10 +00:00
|
|
|
|
|
|
|
if (buf[0] == '1')
|
|
|
|
{
|
|
|
|
if (buf[1] == '0')
|
|
|
|
{
|
|
|
|
if (buf[2] == '0' && buf[3] == ':')
|
|
|
|
{
|
|
|
|
// 100. Human-readable comment line. Ignore
|
|
|
|
}
|
|
|
|
else if (buf[2] == '1' && buf[3] == ':')
|
|
|
|
{
|
|
|
|
// 101. Human-readable information line.
|
|
|
|
mComment += (buf + 4);
|
|
|
|
}
|
|
|
|
else if (buf[2] == '2' && buf[3] == ':')
|
|
|
|
{
|
|
|
|
// 102. Human-readable information line, HTML.
|
|
|
|
mComment += (buf + 4);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (buf[0] == '2')
|
|
|
|
{
|
|
|
|
if (buf[1] == '0')
|
|
|
|
{
|
|
|
|
if (buf[2] == '0' && buf[3] == ':')
|
|
|
|
{
|
|
|
|
// 200. Define field names
|
|
|
|
rv = ParseFormat(buf + 4);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
}
|
|
|
|
else if (buf[2] == '1' && buf[3] == ':')
|
|
|
|
{
|
|
|
|
// 201. Field data
|
|
|
|
rv = ParseData(buf + 4);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (buf[0] == '3')
|
|
|
|
{
|
|
|
|
if (buf[1] == '0')
|
|
|
|
{
|
|
|
|
if (buf[2] == '0' && buf[3] == ':')
|
|
|
|
{
|
|
|
|
// 300. Self-referring URL
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return(NS_OK);
|
1999-08-23 03:48:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-10 23:51:10 +00:00
|
|
|
|
1999-08-23 03:48:53 +00:00
|
|
|
nsresult
|
|
|
|
nsHTTPIndexParser::ParseFormat(const char* aFormatStr)
|
|
|
|
{
|
|
|
|
// Parse a "200" format line, and remember the fields and their
|
|
|
|
// ordering in mFormat. Multiple 200 lines stomp on each other.
|
|
|
|
mFormat.Clear();
|
|
|
|
|
|
|
|
do {
|
2000-03-12 09:14:14 +00:00
|
|
|
while (*aFormatStr && nsCRT::IsAsciiSpace(PRUnichar(*aFormatStr)))
|
1999-08-23 03:48:53 +00:00
|
|
|
++aFormatStr;
|
|
|
|
|
1999-11-10 23:51:10 +00:00
|
|
|
if (! *aFormatStr)
|
1999-08-23 03:48:53 +00:00
|
|
|
break;
|
|
|
|
|
1999-08-24 04:16:33 +00:00
|
|
|
nsCAutoString name;
|
1999-11-10 23:51:10 +00:00
|
|
|
PRInt32 len = 0;
|
2000-03-12 09:14:14 +00:00
|
|
|
while (aFormatStr[len] && !nsCRT::IsAsciiSpace(PRUnichar(aFormatStr[len])))
|
1999-11-10 23:51:10 +00:00
|
|
|
++len;
|
|
|
|
name.SetCapacity(len + 1);
|
|
|
|
name.Append(aFormatStr, len);
|
|
|
|
aFormatStr += len;
|
1999-08-24 04:16:33 +00:00
|
|
|
|
|
|
|
// Okay, we're gonna monkey with the nsStr. Bold!
|
|
|
|
name.mLength = nsUnescapeCount(name.mStr);
|
1999-08-23 03:48:53 +00:00
|
|
|
|
1999-08-26 05:54:04 +00:00
|
|
|
Field* field = nsnull;
|
|
|
|
for (Field* i = gFieldTable; i->mName; ++i) {
|
|
|
|
if (name.EqualsIgnoreCase(i->mName)) {
|
|
|
|
field = i;
|
1999-08-23 03:48:53 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-08-26 05:54:04 +00:00
|
|
|
mFormat.AppendElement(field);
|
1999-08-23 03:48:53 +00:00
|
|
|
} while (*aFormatStr);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-10 23:51:10 +00:00
|
|
|
|
1999-08-23 03:48:53 +00:00
|
|
|
nsresult
|
|
|
|
nsHTTPIndexParser::ParseData(const char* aDataStr)
|
|
|
|
{
|
|
|
|
// Parse a "201" data line, using the field ordering specified in
|
|
|
|
// mFormat.
|
|
|
|
|
|
|
|
if (mFormat.Count() == 0) {
|
|
|
|
// Ignore if we haven't seen a format yet.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-08-25 01:32:40 +00:00
|
|
|
// Because the 'file:' protocol won't do this for us, we need to
|
|
|
|
// make sure that the mDirectoryURI ends with a '/' before
|
|
|
|
// concatenating.
|
|
|
|
nsresult rv;
|
2000-02-03 03:44:22 +00:00
|
|
|
NS_WITH_SERVICE(nsIIOService, ioServ, kIOServiceCID, &rv);
|
1999-08-25 01:32:40 +00:00
|
|
|
nsCOMPtr<nsIURI> realbase;
|
|
|
|
|
|
|
|
{
|
|
|
|
nsXPIDLCString orig;
|
|
|
|
rv = mDirectoryURI->GetSpec(getter_Copies(orig));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
nsCAutoString basestr(orig);
|
|
|
|
if (basestr.Last() != '/')
|
|
|
|
basestr.Append('/');
|
|
|
|
|
|
|
|
rv = NS_NewURI(getter_AddRefs(realbase), (const char*) basestr);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-08-23 03:48:53 +00:00
|
|
|
// First, we'll iterate through the values and remember each (using
|
|
|
|
// an array of autostrings allocated on the stack, if possible). We
|
|
|
|
// have to do this, because we don't know up-front the filename for
|
|
|
|
// which this 201 refers.
|
2000-02-08 01:17:27 +00:00
|
|
|
#define MAX_AUTO_VALUES 8
|
|
|
|
|
1999-08-23 03:48:53 +00:00
|
|
|
nsAutoString autovalues[MAX_AUTO_VALUES];
|
|
|
|
nsAutoString* values = autovalues;
|
|
|
|
|
|
|
|
if (mFormat.Count() > MAX_AUTO_VALUES) {
|
2000-02-08 01:17:27 +00:00
|
|
|
// Yeah, we really -do- want to create nsAutoStrings in the heap
|
|
|
|
// here, because most of the fields will be l.t. 32 characters:
|
|
|
|
// this avoids an extra allocation for the nsString's buffer.
|
1999-08-23 03:48:53 +00:00
|
|
|
values = new nsAutoString[mFormat.Count()];
|
|
|
|
if (! values)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
1999-08-25 01:32:40 +00:00
|
|
|
rv = NS_OK;
|
1999-08-23 03:48:53 +00:00
|
|
|
nsCOMPtr<nsIRDFResource> entry;
|
|
|
|
|
1999-08-26 05:54:04 +00:00
|
|
|
for (PRInt32 i = 0; i < mFormat.Count(); ++i) {
|
1999-08-23 03:48:53 +00:00
|
|
|
// If we've exhausted the data before we run out of fields, just
|
|
|
|
// bail.
|
|
|
|
if (! *aDataStr)
|
|
|
|
break;
|
|
|
|
|
2000-03-12 09:14:14 +00:00
|
|
|
while (*aDataStr && nsCRT::IsAsciiSpace(PRUnichar(*aDataStr)))
|
1999-08-23 03:48:53 +00:00
|
|
|
++aDataStr;
|
|
|
|
|
1999-11-10 23:51:10 +00:00
|
|
|
nsCAutoString value;
|
|
|
|
PRInt32 len;
|
1999-08-23 03:48:53 +00:00
|
|
|
|
|
|
|
if (*aDataStr == '"' || *aDataStr == '\'') {
|
|
|
|
// it's a quoted string. snarf everything up to the next quote character
|
|
|
|
const char quotechar = *(aDataStr++);
|
1999-11-10 23:51:10 +00:00
|
|
|
len = 0;
|
|
|
|
while (aDataStr[len] && (aDataStr[len] != quotechar))
|
|
|
|
++len;
|
|
|
|
value.SetCapacity(len + 1);
|
|
|
|
value.Append(aDataStr, len);
|
|
|
|
aDataStr += len;
|
1999-08-23 03:48:53 +00:00
|
|
|
|
|
|
|
if (! aDataStr) {
|
|
|
|
NS_WARNING("quoted value not terminated");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// it's unquoted. snarf until we see whitespace.
|
1999-11-10 23:51:10 +00:00
|
|
|
len = 0;
|
2000-03-12 09:14:14 +00:00
|
|
|
while (aDataStr[len] && !nsCRT::IsAsciiSpace(aDataStr[len]))
|
1999-11-10 23:51:10 +00:00
|
|
|
++len;
|
|
|
|
value.SetCapacity(len + 1);
|
|
|
|
value.Append(aDataStr, len);
|
|
|
|
aDataStr += len;
|
1999-08-23 03:48:53 +00:00
|
|
|
}
|
|
|
|
|
1999-08-24 04:16:33 +00:00
|
|
|
// Monkey with the nsStr, because we're bold.
|
|
|
|
value.mLength = nsUnescapeCount(value.mStr);
|
|
|
|
|
1999-08-26 05:54:04 +00:00
|
|
|
values[i] = value;
|
1999-08-23 03:48:53 +00:00
|
|
|
|
1999-08-26 05:54:04 +00:00
|
|
|
Field* field = NS_STATIC_CAST(Field*, mFormat.ElementAt(i));
|
|
|
|
if (field && field->mProperty == kHTTPIndex_Filename) {
|
1999-08-23 03:48:53 +00:00
|
|
|
// we found the filename; construct a resource for its entry
|
|
|
|
nsAutoString entryuri;
|
2000-02-03 03:44:22 +00:00
|
|
|
char* result = nsnull;
|
|
|
|
rv = ioServ->Escape(value.mStr, nsIIOService::url_FileBaseName,
|
|
|
|
&result);
|
|
|
|
rv = NS_MakeAbsoluteURI(result, realbase, entryuri);
|
|
|
|
CRTFREEIF(result);
|
1999-08-23 03:48:53 +00:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "unable make absolute URI");
|
|
|
|
if (NS_FAILED(rv)) break;
|
|
|
|
rv = gRDF->GetResource(nsCAutoString(entryuri), getter_AddRefs(entry));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// At this point, we'll (hopefully) have found the filename and
|
|
|
|
// constructed a resource for it, stored in entry. So now take a
|
|
|
|
// second pass through the values and add as statements to the RDF
|
|
|
|
// datasource.
|
|
|
|
if (entry && NS_SUCCEEDED(rv)) {
|
|
|
|
for (PRInt32 indx = 0; indx < mFormat.Count(); ++indx) {
|
1999-08-26 05:54:04 +00:00
|
|
|
Field* field = NS_REINTERPRET_CAST(Field*, mFormat.ElementAt(indx));
|
|
|
|
if (! field)
|
1999-08-23 03:48:53 +00:00
|
|
|
continue;
|
|
|
|
|
1999-08-26 05:54:04 +00:00
|
|
|
nsCOMPtr<nsIRDFNode> value;
|
1999-11-10 23:51:10 +00:00
|
|
|
rv = (*field->mParse)(field->mProperty, values[indx], getter_AddRefs(value));
|
1999-08-23 03:48:53 +00:00
|
|
|
if (NS_FAILED(rv)) break;
|
1999-11-10 23:51:10 +00:00
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
rv = mDataSource->Assert(entry, field->mProperty, value, PR_TRUE);
|
|
|
|
if (NS_FAILED(rv)) break;
|
|
|
|
}
|
1999-08-23 03:48:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rv = mDirectoryContainer->AppendElement(entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we needed to spill values onto the heap, make sure we clean up
|
|
|
|
// here.
|
|
|
|
if (values != autovalues)
|
|
|
|
delete[] values;
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-10 23:51:10 +00:00
|
|
|
|
1999-08-26 05:54:04 +00:00
|
|
|
nsresult
|
1999-11-10 23:56:29 +00:00
|
|
|
nsHTTPIndexParser::ParseLiteral(nsIRDFResource *arc, nsString& aValue, nsIRDFNode** aResult)
|
1999-08-26 05:54:04 +00:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
1999-11-10 23:51:10 +00:00
|
|
|
if (arc == kHTTPIndex_Filename)
|
|
|
|
{
|
|
|
|
// strip off trailing slash(s) from directory names
|
|
|
|
PRInt32 len = aValue.Length();
|
|
|
|
if (len > 0)
|
|
|
|
{
|
|
|
|
if (aValue[len - 1] == '/')
|
|
|
|
{
|
1999-11-10 23:56:29 +00:00
|
|
|
aValue.SetLength(len - 1);
|
1999-11-10 23:51:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-08-26 05:54:04 +00:00
|
|
|
nsCOMPtr<nsIRDFLiteral> result;
|
|
|
|
rv = gRDF->GetLiteral(aValue.GetUnicode(), getter_AddRefs(result));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2000-01-11 20:49:15 +00:00
|
|
|
return result->QueryInterface(NS_GET_IID(nsIRDFNode), (void**) aResult);
|
1999-08-26 05:54:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-10 23:51:10 +00:00
|
|
|
|
1999-08-26 05:54:04 +00:00
|
|
|
nsresult
|
1999-11-10 23:56:29 +00:00
|
|
|
nsHTTPIndexParser::ParseDate(nsIRDFResource *arc, nsString& aValue, nsIRDFNode** aResult)
|
1999-08-26 05:54:04 +00:00
|
|
|
{
|
|
|
|
PRTime tm;
|
2000-02-09 05:08:54 +00:00
|
|
|
PRStatus err = PR_ParseTimeString(nsCAutoString(aValue), PR_FALSE, &tm);
|
1999-08-26 05:54:04 +00:00
|
|
|
if (err != PR_SUCCESS)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIRDFDate> result;
|
|
|
|
rv = gRDF->GetDateLiteral(tm, getter_AddRefs(result));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2000-01-11 20:49:15 +00:00
|
|
|
return result->QueryInterface(NS_GET_IID(nsIRDFNode), (void**) aResult);
|
1999-08-26 05:54:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-10 23:51:10 +00:00
|
|
|
|
1999-08-26 05:54:04 +00:00
|
|
|
nsresult
|
1999-11-10 23:56:29 +00:00
|
|
|
nsHTTPIndexParser::ParseInt(nsIRDFResource *arc, nsString& aValue, nsIRDFNode** aResult)
|
1999-08-26 05:54:04 +00:00
|
|
|
{
|
|
|
|
PRInt32 err;
|
|
|
|
PRInt32 i = aValue.ToInteger(&err);
|
|
|
|
if (nsresult(err) != NS_OK)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
1999-11-10 23:51:10 +00:00
|
|
|
if (i == 0)
|
|
|
|
{
|
|
|
|
// disregard "zero" values
|
|
|
|
*aResult = nsnull;
|
|
|
|
return(NS_OK);
|
|
|
|
}
|
|
|
|
|
1999-08-26 05:54:04 +00:00
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIRDFInt> result;
|
|
|
|
rv = gRDF->GetIntLiteral(i, getter_AddRefs(result));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2000-01-11 20:49:15 +00:00
|
|
|
return result->QueryInterface(NS_GET_IID(nsIRDFNode), (void**) aResult);
|
1999-08-26 05:54:04 +00:00
|
|
|
}
|
|
|
|
|
1999-11-10 23:51:10 +00:00
|
|
|
|
|
|
|
|
2000-02-08 01:17:27 +00:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
//
|
1999-08-23 03:48:53 +00:00
|
|
|
// nsHTTPIndex implementation
|
2000-02-08 01:17:27 +00:00
|
|
|
//
|
1999-08-23 03:48:53 +00:00
|
|
|
|
1999-11-13 07:36:28 +00:00
|
|
|
nsHTTPIndex::nsHTTPIndex(nsISupports* aContainer)
|
1999-08-23 03:48:53 +00:00
|
|
|
: mContainer(aContainer)
|
|
|
|
{
|
|
|
|
NS_INIT_REFCNT();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-10 23:51:10 +00:00
|
|
|
|
1999-08-23 03:48:53 +00:00
|
|
|
nsresult
|
|
|
|
nsHTTPIndex::Init(nsIURI* aBaseURL)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(aBaseURL != nsnull, "null ptr");
|
|
|
|
if (! aBaseURL)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
1999-08-25 01:32:40 +00:00
|
|
|
nsXPIDLCString url;
|
|
|
|
rv = aBaseURL->GetSpec(getter_Copies(url));
|
1999-08-23 03:48:53 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
1999-08-25 01:32:40 +00:00
|
|
|
mBaseURL = url;
|
|
|
|
|
1999-08-23 03:48:53 +00:00
|
|
|
static NS_DEFINE_CID(kRDFInMemoryDataSourceCID, NS_RDFINMEMORYDATASOURCE_CID);
|
1999-08-24 04:16:33 +00:00
|
|
|
rv = nsComponentManager::CreateInstance(kRDFInMemoryDataSourceCID, nsnull,
|
2000-01-11 20:49:15 +00:00
|
|
|
NS_GET_IID(nsIRDFDataSource),
|
1999-08-23 03:48:53 +00:00
|
|
|
getter_AddRefs(mDataSource));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-10 23:51:10 +00:00
|
|
|
|
1999-08-23 03:48:53 +00:00
|
|
|
nsresult
|
1999-11-13 07:36:28 +00:00
|
|
|
nsHTTPIndex::Create(nsIURI* aBaseURL, nsISupports* aContainer, nsIHTTPIndex** aResult)
|
1999-08-23 03:48:53 +00:00
|
|
|
{
|
|
|
|
nsHTTPIndex* result = new nsHTTPIndex(aContainer);
|
|
|
|
if (! result)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
nsresult rv = result->Init(aBaseURL);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
delete result;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ADDREF(result);
|
|
|
|
*aResult = result;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-11-10 23:51:10 +00:00
|
|
|
|
|
|
|
|
1999-08-23 03:48:53 +00:00
|
|
|
nsHTTPIndex::~nsHTTPIndex()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
1999-11-10 23:51:10 +00:00
|
|
|
|
2000-02-08 01:17:27 +00:00
|
|
|
NS_IMPL_ISUPPORTS1(nsHTTPIndex, nsIHTTPIndex);
|
1999-11-10 23:51:10 +00:00
|
|
|
|
|
|
|
|
1999-08-23 03:48:53 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTTPIndex::GetBaseURL(char** _result)
|
|
|
|
{
|
|
|
|
*_result = nsXPIDLCString::Copy(mBaseURL);
|
|
|
|
if (! *_result)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-10 23:51:10 +00:00
|
|
|
|
1999-08-23 03:48:53 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTTPIndex::GetDataSource(nsIRDFDataSource** _result)
|
|
|
|
{
|
|
|
|
*_result = mDataSource;
|
|
|
|
NS_IF_ADDREF(*_result);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-11-10 23:51:10 +00:00
|
|
|
|
|
|
|
|
1999-08-23 03:48:53 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTTPIndex::CreateListener(nsIStreamListener** _result)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
rv = nsHTTPIndexParser::Create(this, mContainer, _result);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
// Clear this: we only need to pass it through once.
|
|
|
|
mContainer = nsnull;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-02-08 01:17:27 +00:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
//
|
1999-08-23 03:48:53 +00:00
|
|
|
// nsDirectoryViewerFactory
|
|
|
|
//
|
|
|
|
|
|
|
|
class nsDirectoryViewerFactory : public nsIDocumentLoaderFactory
|
|
|
|
{
|
1999-08-23 04:19:48 +00:00
|
|
|
protected:
|
1999-08-23 03:48:53 +00:00
|
|
|
nsDirectoryViewerFactory();
|
|
|
|
virtual ~nsDirectoryViewerFactory();
|
|
|
|
|
|
|
|
public:
|
|
|
|
// constructor
|
|
|
|
static NS_IMETHODIMP
|
|
|
|
Create(nsISupports* aOuter, REFNSIID aIID, void** aResult);
|
|
|
|
|
|
|
|
// nsISupports interface
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
// nsIDocumentLoaderFactory interface
|
|
|
|
NS_IMETHOD CreateInstance(const char *aCommand,
|
|
|
|
nsIChannel* aChannel,
|
|
|
|
nsILoadGroup* aLoadGroup,
|
|
|
|
const char* aContentType,
|
1999-11-15 07:29:23 +00:00
|
|
|
nsISupports* aContainer,
|
1999-08-23 03:48:53 +00:00
|
|
|
nsISupports* aExtraInfo,
|
|
|
|
nsIStreamListener** aDocListenerResult,
|
|
|
|
nsIContentViewer** aDocViewerResult);
|
|
|
|
|
1999-11-15 07:29:23 +00:00
|
|
|
NS_IMETHOD CreateInstanceForDocument(nsISupports* aContainer,
|
1999-08-23 03:48:53 +00:00
|
|
|
nsIDocument* aDocument,
|
|
|
|
const char *aCommand,
|
|
|
|
nsIContentViewer** aDocViewerResult);
|
|
|
|
};
|
|
|
|
|
1999-11-10 23:51:10 +00:00
|
|
|
|
|
|
|
|
1999-08-23 03:48:53 +00:00
|
|
|
nsDirectoryViewerFactory::nsDirectoryViewerFactory()
|
|
|
|
{
|
|
|
|
NS_INIT_REFCNT();
|
|
|
|
}
|
|
|
|
|
1999-11-10 23:51:10 +00:00
|
|
|
|
|
|
|
|
1999-08-23 03:48:53 +00:00
|
|
|
nsDirectoryViewerFactory::~nsDirectoryViewerFactory()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-10 23:51:10 +00:00
|
|
|
|
1999-08-23 03:48:53 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDirectoryViewerFactory::Create(nsISupports* aOuter, REFNSIID aIID, void** aResult)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(aOuter == nsnull, "no aggregation");
|
|
|
|
if (aOuter)
|
|
|
|
return NS_ERROR_NO_AGGREGATION;
|
|
|
|
|
|
|
|
nsDirectoryViewerFactory* result
|
|
|
|
= new nsDirectoryViewerFactory();
|
|
|
|
|
|
|
|
if (! result)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
NS_ADDREF(result); // stabilize
|
|
|
|
nsresult rv = result->QueryInterface(aIID, aResult);
|
|
|
|
NS_RELEASE(result);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2000-02-08 01:17:27 +00:00
|
|
|
NS_IMPL_ISUPPORTS1(nsDirectoryViewerFactory, nsIDocumentLoaderFactory);
|
1999-11-10 23:51:10 +00:00
|
|
|
|
|
|
|
|
1999-08-23 03:48:53 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDirectoryViewerFactory::CreateInstance(const char *aCommand,
|
|
|
|
nsIChannel* aChannel,
|
|
|
|
nsILoadGroup* aLoadGroup,
|
|
|
|
const char* aContentType,
|
1999-11-15 07:29:23 +00:00
|
|
|
nsISupports* aContainer,
|
1999-08-23 03:48:53 +00:00
|
|
|
nsISupports* aExtraInfo,
|
|
|
|
nsIStreamListener** aDocListenerResult,
|
|
|
|
nsIContentViewer** aDocViewerResult)
|
|
|
|
{
|
2000-01-13 23:25:25 +00:00
|
|
|
// This is where we shunt the HTTP/Index stream into our datasource,
|
|
|
|
// and open the directory viewer XUL file as the content stream to
|
|
|
|
// load in its place.
|
1999-08-23 03:48:53 +00:00
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
// Create a dummy loader that will load a stub XUL document.
|
|
|
|
nsCOMPtr<nsIDocumentLoaderFactory> factory;
|
|
|
|
rv = nsComponentManager::CreateInstance(NS_DOCUMENT_LOADER_FACTORY_PROGID_PREFIX "view/text/xul",
|
|
|
|
nsnull,
|
2000-01-11 20:49:15 +00:00
|
|
|
NS_GET_IID(nsIDocumentLoaderFactory),
|
1999-08-23 03:48:53 +00:00
|
|
|
getter_AddRefs(factory));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
rv = NS_NewURI(getter_AddRefs(uri), "chrome://directory/content/");
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIChannel> channel;
|
1999-08-26 22:45:55 +00:00
|
|
|
rv = NS_OpenURI(getter_AddRefs(channel), uri, aLoadGroup);
|
1999-08-23 03:48:53 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIStreamListener> listener;
|
|
|
|
rv = factory->CreateInstance("view", channel, aLoadGroup, "text/xul",
|
|
|
|
aContainer, aExtraInfo, getter_AddRefs(listener),
|
|
|
|
aDocViewerResult);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
rv = channel->AsyncRead(0, -1, nsnull, listener);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
// Create an HTTPIndex object so that we can stuff it into the script context
|
|
|
|
nsCOMPtr<nsIURI> baseuri;
|
|
|
|
rv = aChannel->GetURI(getter_AddRefs(baseuri));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIHTTPIndex> httpindex;
|
|
|
|
rv = nsHTTPIndex::Create(baseuri, aContainer, getter_AddRefs(httpindex));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
// Now shanghai the stream into our http-index parsing datasource
|
|
|
|
// wrapper beastie.
|
|
|
|
rv = httpindex->CreateListener(aDocListenerResult);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
1999-08-24 04:16:33 +00:00
|
|
|
// addref the nsIHTTPIndex so that it'll live long enough to get
|
|
|
|
// added to the embedder's script global object; this happens when
|
|
|
|
// the stream's OnStartRequest() is called. (We can feel safe that
|
|
|
|
// this'll happen because of the flow-of-control in nsWebShell.cpp)
|
|
|
|
nsIHTTPIndex* dummy = httpindex;
|
|
|
|
NS_ADDREF(dummy);
|
|
|
|
|
1999-08-23 03:48:53 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-11-10 23:51:10 +00:00
|
|
|
|
|
|
|
|
1999-08-23 03:48:53 +00:00
|
|
|
NS_IMETHODIMP
|
1999-11-15 07:29:23 +00:00
|
|
|
nsDirectoryViewerFactory::CreateInstanceForDocument(nsISupports* aContainer,
|
1999-08-23 03:48:53 +00:00
|
|
|
nsIDocument* aDocument,
|
|
|
|
const char *aCommand,
|
|
|
|
nsIContentViewer** aDocViewerResult)
|
|
|
|
{
|
|
|
|
NS_NOTYETIMPLEMENTED("didn't expect to get here");
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
// Component Exports
|
|
|
|
|
1999-10-05 23:39:48 +00:00
|
|
|
// The list of components we register
|
2000-02-08 05:39:23 +00:00
|
|
|
static nsModuleComponentInfo components[] = {
|
|
|
|
{ "Directory Viewer", NS_DIRECTORYVIEWERFACTORY_CID,
|
|
|
|
NS_DOCUMENT_LOADER_FACTORY_PROGID_PREFIX "view/application/http-index-format",
|
|
|
|
nsDirectoryViewerFactory::Create,
|
|
|
|
},
|
1999-10-05 23:39:48 +00:00
|
|
|
};
|
1999-08-23 03:48:53 +00:00
|
|
|
|
2000-02-08 05:39:23 +00:00
|
|
|
NS_IMPL_NSGETMODULE("nsDirectoryViewerModule", components)
|