зеркало из https://github.com/mozilla/gecko-dev.git
fix mem leak of the http handler and the http request objects.
Add GetnsAuthEngine to nsIHTTPProtocolHandler, remove nsHTTPHandler::GetInstance, force callers to go through the service manager. remove the http handler factory code since we just need the http module r=warren
This commit is contained in:
Родитель
ff0ee4c6c9
Коммит
aabdc49dd6
|
@ -24,6 +24,12 @@
|
|||
|
||||
interface nsIInputStream;
|
||||
|
||||
[ptr] native nsAuthEnginePtr(nsAuthEngine);
|
||||
|
||||
%{C++
|
||||
class nsAuthEngine;
|
||||
%}
|
||||
|
||||
[scriptable, uuid(ad1d53ae-3e25-11d3-8cd3-0060b0fc14a3)]
|
||||
interface nsIHTTPProtocolHandler : nsIProtocolHandler
|
||||
{
|
||||
|
@ -49,6 +55,10 @@ interface nsIHTTPProtocolHandler : nsIProtocolHandler
|
|||
nsIInputStream NewPostDataStream(in boolean isFile,
|
||||
in string data,
|
||||
in unsigned long encodeFlags);
|
||||
|
||||
/* i'll file a bug on the http guys to make nsAuthEngine scriptable
|
||||
and then we can get rid of this no script code! */
|
||||
[noscript] void getAuthEngine(out nsAuthEnginePtr authEngine);
|
||||
|
||||
};
|
||||
|
||||
|
@ -58,8 +68,4 @@ interface nsIHTTPProtocolHandler : nsIProtocolHandler
|
|||
#define NS_ERROR_BAD_REQUEST NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_NETWORK, 200)
|
||||
#define NS_ERROR_BUSY NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_NETWORK, 201)
|
||||
|
||||
|
||||
// Create (or get) HTTP Handler
|
||||
extern NS_METHOD NS_CreateOrGetHTTPHandler(nsIHTTPProtocolHandler* *o_HTTPHandler);
|
||||
|
||||
%}
|
||||
|
|
|
@ -65,26 +65,6 @@ static NS_DEFINE_CID(kStandardUrlCID, NS_STANDARDURL_CID);
|
|||
static NS_DEFINE_CID(kSocketTransportServiceCID, NS_SOCKETTRANSPORTSERVICE_CID);
|
||||
static NS_DEFINE_CID(kPrefServiceCID, NS_PREF_CID);
|
||||
|
||||
NS_METHOD NS_CreateOrGetHTTPHandler(nsIHTTPProtocolHandler* *o_HTTPHandler)
|
||||
{
|
||||
#if defined(PR_LOGGING)
|
||||
//
|
||||
// Initialize the global PRLogModule for HTTP Protocol logging
|
||||
// if necessary...
|
||||
//
|
||||
if (nsnull == gHTTPLog) {
|
||||
gHTTPLog = PR_NewLogModule("nsHTTPProtocol");
|
||||
}
|
||||
#endif /* PR_LOGGING */
|
||||
|
||||
if (o_HTTPHandler)
|
||||
{
|
||||
*o_HTTPHandler = nsHTTPHandler::GetInstance();
|
||||
return NS_OK;
|
||||
}
|
||||
return NS_ERROR_NULL_POINTER;
|
||||
}
|
||||
|
||||
nsHTTPHandler::nsHTTPHandler():
|
||||
mDoKeepAlive(PR_FALSE),
|
||||
mProxy(nsnull),
|
||||
|
@ -93,6 +73,10 @@ nsHTTPHandler::nsHTTPHandler():
|
|||
nsresult rv;
|
||||
NS_INIT_REFCNT();
|
||||
|
||||
#if defined (PR_LOGGING)
|
||||
gHTTPLog = PR_NewLogModule("nsHTTPProtocol");
|
||||
#endif /* PR_LOGGING */
|
||||
|
||||
mSessionStartTime = PR_Now();
|
||||
|
||||
PR_LOG(gHTTPLog, PR_LOG_ALWAYS,
|
||||
|
@ -501,14 +485,6 @@ nsresult nsHTTPHandler::CreateTransport(const char* host,
|
|||
o_pTrans);
|
||||
}
|
||||
|
||||
nsHTTPHandler * nsHTTPHandler::GetInstance(void)
|
||||
{
|
||||
static nsHTTPHandler* pHandler = new nsHTTPHandler();
|
||||
NS_IF_ADDREF(pHandler);
|
||||
return pHandler;
|
||||
};
|
||||
|
||||
|
||||
nsresult nsHTTPHandler::ReleaseTransport(nsIChannel* i_pTrans)
|
||||
{
|
||||
nsresult rv;
|
||||
|
@ -628,3 +604,10 @@ nsHTTPHandler::SetProxyHost(const char* i_ProxyHost)
|
|||
}
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsHTTPHandler::GetAuthEngine(nsAuthEngine** o_AuthEngine)
|
||||
{
|
||||
*o_AuthEngine = &mAuthEngine;
|
||||
return NS_OK;
|
||||
}
|
||||
|
|
|
@ -123,9 +123,6 @@ public:
|
|||
*/
|
||||
NS_IMETHOD FollowRedirects(PRBool bFollow=PR_TRUE);
|
||||
|
||||
// Singleton function - note this AddRefs so when you are done... release it!
|
||||
static nsHTTPHandler* GetInstance(void);
|
||||
|
||||
// Functions from nsIHTTPProtocolHandler
|
||||
NS_DECL_NSIHTTPPROTOCOLHANDLER
|
||||
|
||||
|
@ -156,19 +153,13 @@ public:
|
|||
virtual nsresult ReleaseTransport(nsIChannel* i_pTrans);
|
||||
|
||||
virtual nsresult CancelPendingChannel(nsHTTPChannel* aChannel);
|
||||
|
||||
virtual nsresult GetAuthEngine(nsAuthEngine** o_AuthEngine)
|
||||
{
|
||||
*o_AuthEngine = &mAuthEngine;
|
||||
return NS_OK;
|
||||
};
|
||||
|
||||
PRTime GetSessionStartTime() { return mSessionStartTime; }
|
||||
|
||||
nsHTTPHandler(void);
|
||||
protected:
|
||||
|
||||
// None
|
||||
nsHTTPHandler(void);
|
||||
virtual ~nsHTTPHandler();
|
||||
|
||||
// This is the array of connections that the handler thread
|
||||
|
|
|
@ -21,13 +21,7 @@
|
|||
*/
|
||||
|
||||
/*
|
||||
|
||||
The nsHTTPHandlerFactory implementation. This was directly
|
||||
plagiarized from Chris Waterson the Great. So if you find
|
||||
a fault here... make sure you notify him as well.
|
||||
|
||||
-Gagan Saksena 03/25/99
|
||||
|
||||
This file has become obsolete...it's been replaced by the http handler module
|
||||
*/
|
||||
|
||||
#include "nsCOMPtr.h"
|
||||
|
@ -44,76 +38,3 @@ static NS_DEFINE_IID(kIFactoryIID, NS_IFACTORY_IID);
|
|||
static NS_DEFINE_CID(kComponentManagerCID, NS_COMPONENTMANAGER_CID);
|
||||
static NS_DEFINE_CID(kHTTPHandlerCID, NS_HTTP_HANDLER_FACTORY_CID);
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
|
||||
nsHTTPHandlerFactory::nsHTTPHandlerFactory(const nsCID &aClass)
|
||||
: mClassID(aClass)
|
||||
{
|
||||
NS_INIT_REFCNT();
|
||||
}
|
||||
|
||||
nsHTTPHandlerFactory::~nsHTTPHandlerFactory()
|
||||
{
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsHTTPHandlerFactory::QueryInterface(const nsIID &aIID, void **aResult)
|
||||
{
|
||||
if (! aResult)
|
||||
return NS_ERROR_NULL_POINTER;
|
||||
|
||||
// Always NULL result, in case of failure
|
||||
*aResult = nsnull;
|
||||
|
||||
if (aIID.Equals(NS_GET_IID(nsISupports))) {
|
||||
*aResult = NS_STATIC_CAST(nsISupports*, this);
|
||||
AddRef();
|
||||
return NS_OK;
|
||||
} else if (aIID.Equals(kIFactoryIID)) {
|
||||
*aResult = NS_STATIC_CAST(nsIFactory*, this);
|
||||
AddRef();
|
||||
return NS_OK;
|
||||
}
|
||||
return NS_NOINTERFACE;
|
||||
}
|
||||
|
||||
NS_IMPL_ADDREF(nsHTTPHandlerFactory);
|
||||
NS_IMPL_RELEASE(nsHTTPHandlerFactory);
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsHTTPHandlerFactory::CreateInstance(nsISupports *aOuter,
|
||||
const nsIID &aIID,
|
||||
void **aResult)
|
||||
{
|
||||
if (! aResult)
|
||||
return NS_ERROR_NULL_POINTER;
|
||||
|
||||
if (aOuter)
|
||||
return NS_ERROR_NO_AGGREGATION;
|
||||
|
||||
*aResult = nsnull;
|
||||
|
||||
nsresult rv;
|
||||
|
||||
nsISupports *inst = nsnull;
|
||||
if (mClassID.Equals(kHTTPHandlerCID)) {
|
||||
if (NS_FAILED(rv = NS_CreateOrGetHTTPHandler((nsIHTTPProtocolHandler**) &inst)))
|
||||
return rv;
|
||||
}
|
||||
else {
|
||||
return NS_ERROR_NO_INTERFACE;
|
||||
}
|
||||
|
||||
if (!inst)
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
|
||||
inst->QueryInterface(aIID, aResult);
|
||||
NS_RELEASE(inst);
|
||||
return rv;
|
||||
}
|
||||
|
||||
nsresult nsHTTPHandlerFactory::LockFactory(PRBool aLock)
|
||||
{
|
||||
// Not implemented in simplest case.
|
||||
return NS_OK;
|
||||
}
|
||||
|
|
|
@ -24,26 +24,4 @@
|
|||
|
||||
#include "nsIFactory.h"
|
||||
|
||||
class nsHTTPHandlerFactory : public nsIFactory
|
||||
{
|
||||
public:
|
||||
nsHTTPHandlerFactory(const nsCID &aClass);
|
||||
|
||||
// nsISupports methods
|
||||
NS_DECL_ISUPPORTS
|
||||
|
||||
// nsIFactory methods
|
||||
NS_IMETHOD CreateInstance(nsISupports *aOuter,
|
||||
const nsIID &aIID,
|
||||
void **aResult);
|
||||
|
||||
NS_IMETHOD LockFactory(PRBool aLock);
|
||||
|
||||
protected:
|
||||
virtual ~nsHTTPHandlerFactory();
|
||||
|
||||
protected:
|
||||
nsCID mClassID;
|
||||
};
|
||||
|
||||
#endif /* nsHTTPHandlerFactory_h___ */
|
||||
#endif
|
||||
|
|
|
@ -20,233 +20,31 @@
|
|||
* Contributor(s):
|
||||
*/
|
||||
#include "nsCOMPtr.h"
|
||||
#include "nsIModule.h"
|
||||
#include "nscore.h"
|
||||
#include "nsIHTTPProtocolHandler.h"
|
||||
#include "nsHTTPCID.h"
|
||||
#include "nsHTTPHandlerFactory.h"
|
||||
#include "nsIComponentManager.h"
|
||||
|
||||
#include "nsIFactory.h"
|
||||
#include "nsIGenericFactory.h"
|
||||
#include "nsIServiceManager.h"
|
||||
#include "nsIModule.h"
|
||||
|
||||
#include "nsXPComFactory.h"
|
||||
#include "nsIProtocolHandler.h" // for NS_NETWORK_PROTOCOL_PROGID_PREFIX
|
||||
|
||||
static NS_DEFINE_CID(kHTTPHandlerCID, NS_HTTP_HANDLER_FACTORY_CID);
|
||||
#include "nsHTTPHandler.h"
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
|
||||
class nsHTTPHandlerModule : public nsIModule
|
||||
NS_GENERIC_FACTORY_CONSTRUCTOR(nsHTTPHandler);
|
||||
|
||||
static nsModuleComponentInfo components[] =
|
||||
{
|
||||
public:
|
||||
nsHTTPHandlerModule();
|
||||
virtual ~nsHTTPHandlerModule();
|
||||
|
||||
NS_DECL_ISUPPORTS
|
||||
|
||||
NS_DECL_NSIMODULE
|
||||
|
||||
protected:
|
||||
nsresult Initialize();
|
||||
|
||||
void Shutdown();
|
||||
|
||||
PRBool mInitialized;
|
||||
nsCOMPtr<nsIFactory> mFactory;
|
||||
{ "HTTP Handler",
|
||||
NS_HTTP_HANDLER_FACTORY_CID,
|
||||
NS_NETWORK_PROTOCOL_PROGID_PREFIX "http",
|
||||
nsHTTPHandlerConstructor }
|
||||
};
|
||||
|
||||
static NS_DEFINE_IID(kIModuleIID, NS_IMODULE_IID);
|
||||
|
||||
nsHTTPHandlerModule::nsHTTPHandlerModule()
|
||||
: mInitialized(PR_FALSE)
|
||||
{
|
||||
NS_INIT_ISUPPORTS();
|
||||
}
|
||||
|
||||
nsHTTPHandlerModule::~nsHTTPHandlerModule()
|
||||
{
|
||||
Shutdown();
|
||||
}
|
||||
|
||||
NS_IMPL_ISUPPORTS(nsHTTPHandlerModule, kIModuleIID)
|
||||
|
||||
// Perform our one-time intialization for this module
|
||||
nsresult
|
||||
nsHTTPHandlerModule::Initialize()
|
||||
{
|
||||
if (mInitialized) {
|
||||
return NS_OK;
|
||||
}
|
||||
mInitialized = PR_TRUE;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
// Shutdown this module, releasing all of the module resources
|
||||
void
|
||||
nsHTTPHandlerModule::Shutdown()
|
||||
{
|
||||
// Release the factory object
|
||||
mFactory = nsnull;
|
||||
}
|
||||
|
||||
// Create a factory object for creating instances of aClass.
|
||||
NS_IMETHODIMP
|
||||
nsHTTPHandlerModule::GetClassObject(nsIComponentManager *aCompMgr,
|
||||
const nsCID& aClass,
|
||||
const nsIID& aIID,
|
||||
void** r_classObj)
|
||||
{
|
||||
nsresult rv;
|
||||
|
||||
// Defensive programming: Initialize *r_classObj in case of error below
|
||||
if (!r_classObj) {
|
||||
return NS_ERROR_INVALID_POINTER;
|
||||
}
|
||||
*r_classObj = NULL;
|
||||
|
||||
// Do one-time-only initialization if necessary
|
||||
if (!mInitialized) {
|
||||
rv = Initialize();
|
||||
if (NS_FAILED(rv)) {
|
||||
// Initialization failed! yikes!
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
|
||||
// Choose the appropriate factory, based on the desired instance
|
||||
// class type (aClass).
|
||||
nsCOMPtr<nsIFactory> fact;
|
||||
if (aClass.Equals(kHTTPHandlerCID)) {
|
||||
if (!mFactory) {
|
||||
nsHTTPHandlerFactory* factory = new nsHTTPHandlerFactory(aClass);
|
||||
if (!factory) {
|
||||
rv = NS_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
else {
|
||||
mFactory = factory;
|
||||
}
|
||||
}
|
||||
fact = mFactory;
|
||||
}
|
||||
else {
|
||||
rv = NS_ERROR_FACTORY_NOT_REGISTERED;
|
||||
#ifdef DEBUG
|
||||
char* cs = aClass.ToString();
|
||||
printf("+++ nsHTTPHandlerModule: unable to create factory for %s\n", cs);
|
||||
nsCRT::free(cs);
|
||||
#endif
|
||||
}
|
||||
|
||||
if (fact) {
|
||||
rv = fact->QueryInterface(aIID, r_classObj);
|
||||
}
|
||||
|
||||
return rv;
|
||||
}
|
||||
|
||||
//----------------------------------------
|
||||
|
||||
struct Components {
|
||||
const char* mDescription;
|
||||
const nsID* mCID;
|
||||
const char* mProgID;
|
||||
};
|
||||
|
||||
// The list of components we register
|
||||
static Components gComponents[] = {
|
||||
{ "HTTP Handler", &kHTTPHandlerCID,
|
||||
NS_NETWORK_PROTOCOL_PROGID_PREFIX "http", },
|
||||
};
|
||||
#define NUM_COMPONENTS (sizeof(gComponents) / sizeof(gComponents[0]))
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsHTTPHandlerModule::RegisterSelf(nsIComponentManager *aCompMgr,
|
||||
nsIFileSpec* aPath,
|
||||
const char* registryLocation,
|
||||
const char* componentType)
|
||||
{
|
||||
nsresult rv = NS_OK;
|
||||
|
||||
#ifdef DEBUG
|
||||
printf("*** Registering http: components\n");
|
||||
#endif
|
||||
|
||||
Components* cp = gComponents;
|
||||
Components* end = cp + NUM_COMPONENTS;
|
||||
while (cp < end) {
|
||||
rv = aCompMgr->RegisterComponentSpec(*cp->mCID, cp->mDescription,
|
||||
cp->mProgID, aPath, PR_TRUE,
|
||||
PR_TRUE);
|
||||
if (NS_FAILED(rv)) {
|
||||
#ifdef DEBUG
|
||||
printf("nsHTTPHandlerModule: unable to register %s component => %x\n",
|
||||
cp->mDescription, rv);
|
||||
#endif
|
||||
break;
|
||||
}
|
||||
cp++;
|
||||
}
|
||||
|
||||
return rv;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsHTTPHandlerModule::UnregisterSelf(nsIComponentManager* aCompMgr,
|
||||
nsIFileSpec* aPath,
|
||||
const char* registryLocation)
|
||||
{
|
||||
#ifdef DEBUG
|
||||
printf("*** Unregistering http: components\n");
|
||||
#endif
|
||||
Components* cp = gComponents;
|
||||
Components* end = cp + NUM_COMPONENTS;
|
||||
while (cp < end) {
|
||||
nsresult rv = aCompMgr->UnregisterComponentSpec(*cp->mCID, aPath);
|
||||
if (NS_FAILED(rv)) {
|
||||
#ifdef DEBUG
|
||||
printf("nsHTTPHandlerModule: unable to unregister %s component => %x\n",
|
||||
cp->mDescription, rv);
|
||||
#endif
|
||||
}
|
||||
cp++;
|
||||
}
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsHTTPHandlerModule::CanUnload(nsIComponentManager *aCompMgr, PRBool *okToUnload)
|
||||
{
|
||||
if (!okToUnload) {
|
||||
return NS_ERROR_INVALID_POINTER;
|
||||
}
|
||||
*okToUnload = PR_FALSE;
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------
|
||||
|
||||
static nsHTTPHandlerModule *gModule = NULL;
|
||||
|
||||
extern "C" NS_EXPORT nsresult NSGetModule(nsIComponentManager *servMgr,
|
||||
nsIFileSpec* location,
|
||||
nsIModule** return_cobj)
|
||||
{
|
||||
nsresult rv = NS_OK;
|
||||
|
||||
NS_ASSERTION(return_cobj, "Null argument");
|
||||
NS_ASSERTION(gModule == NULL, "nsHTTPHandlerModule: Module already created.");
|
||||
|
||||
// Create and initialize the module instance
|
||||
nsHTTPHandlerModule *m = new nsHTTPHandlerModule();
|
||||
if (!m) {
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
|
||||
// Increase refcnt and store away nsIModule interface to m in return_cobj
|
||||
rv = m->QueryInterface(NS_GET_IID(nsIModule), (void**)return_cobj);
|
||||
if (NS_FAILED(rv)) {
|
||||
delete m;
|
||||
m = nsnull;
|
||||
}
|
||||
gModule = m; // WARNING: Weak Reference
|
||||
return rv;
|
||||
}
|
||||
|
||||
NS_IMPL_NSGETMODULE("nsHTTPHandlerModule", components)
|
||||
|
|
|
@ -106,10 +106,15 @@ nsHTTPRequest::nsHTTPRequest(nsIURI* i_URL, HTTPMethod i_Method,
|
|||
// Check to see if an authentication header is required
|
||||
nsAuthEngine* pAuthEngine = nsnull;
|
||||
|
||||
if (NS_SUCCEEDED(nsHTTPHandler::GetInstance()->
|
||||
GetAuthEngine(&pAuthEngine)))
|
||||
nsCOMPtr<nsIProtocolHandler> protocolHandler;
|
||||
rv = service->GetProtocolHandler("http", getter_AddRefs(protocolHandler));
|
||||
if (NS_SUCCEEDED(rv))
|
||||
{
|
||||
if (pAuthEngine)
|
||||
nsCOMPtr<nsIHTTPProtocolHandler> httpHandler = do_QueryInterface(protocolHandler);
|
||||
if (httpHandler)
|
||||
{
|
||||
rv = httpHandler->GetAuthEngine(&pAuthEngine);
|
||||
if (NS_SUCCEEDED(rv) && pAuthEngine)
|
||||
{
|
||||
// Qvq lbh xabj gung t?? Ebg13f n yvar va IVZ? Jbj.
|
||||
nsXPIDLCString authStr;
|
||||
|
@ -122,7 +127,10 @@ nsHTTPRequest::nsHTTPRequest(nsIURI* i_URL, HTTPMethod i_Method,
|
|||
SetHeader(nsHTTPAtoms::Authorization, authStr);
|
||||
}
|
||||
}
|
||||
}}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
nsHTTPRequest::~nsHTTPRequest()
|
||||
{
|
||||
|
@ -503,6 +511,7 @@ nsHTTPRequest::OnStopRequest(nsIChannel* channel, nsISupports* i_Context,
|
|||
mConnection->GetResponseDataListener(getter_AddRefs(consumer));
|
||||
|
||||
mConnection->ResponseCompleted(mTransport, consumer, iStatus, i_Msg);
|
||||
|
||||
mTransport = null_nsCOMPtr();
|
||||
|
||||
rv = iStatus;
|
||||
|
|
Загрузка…
Ссылка в новой задаче