From c720caf4645e60ac20355224bfbdb9532927122e Mon Sep 17 00:00:00 2001 From: "warren%netscape.com" Date: Wed, 14 Apr 1999 08:11:10 +0000 Subject: [PATCH] More on http protocol. git-svn-id: svn://10.0.0.236/trunk@27456 18797224-902f-48f8-a5cc-f745e15eee43 --- .../http/src/nsHttpProtocolConnection.cpp | 49 ++++++++++----- .../http/src/nsHttpProtocolConnection.h | 19 +++--- .../http/src/nsHttpProtocolHandler.cpp | 63 +++++++++++++++++-- .../protocol/http/src/nsHttpProtocolHandler.h | 42 ++++++++++++- 4 files changed, 144 insertions(+), 29 deletions(-) diff --git a/mozilla/netwerk/protocol/http/src/nsHttpProtocolConnection.cpp b/mozilla/netwerk/protocol/http/src/nsHttpProtocolConnection.cpp index 95ff9f507c8..827edaa3f14 100644 --- a/mozilla/netwerk/protocol/http/src/nsHttpProtocolConnection.cpp +++ b/mozilla/netwerk/protocol/http/src/nsHttpProtocolConnection.cpp @@ -22,8 +22,10 @@ #include "nsIHttpEventSink.h" #include "nsIComponentManager.h" #include "nsIServiceManager.h" -//#include "nsISocketTransportService.h" +#include "nsISocketTransportService.h" #include "nsIFileTransportService.h" // XXX temporary +#include "nsHttpProtocolHandler.h" +#include "nsITransport.h" //static NS_DEFINE_CID(kSocketTransportServiceCID, NS_SOCKETTRANSPORTSERVICE_CID); static NS_DEFINE_CID(kFileTransportServiceCID, NS_FILETRANSPORTSERVICE_CID); // XXX temporary @@ -33,12 +35,13 @@ static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID); //////////////////////////////////////////////////////////////////////////////// nsHttpProtocolConnection::nsHttpProtocolConnection() - : mUrl(nsnull), mEventSink(nsnull), mState(UNCONNECTED) + : mHandler(nsnull), mUrl(nsnull), mEventSink(nsnull), mState(UNCONNECTED) { } nsHttpProtocolConnection::~nsHttpProtocolConnection() { + NS_IF_RELEASE(mHandler); NS_IF_RELEASE(mUrl); NS_IF_RELEASE(mEventSink); } @@ -67,10 +70,14 @@ nsHttpProtocolConnection::QueryInterface(const nsIID& aIID, void** aInstancePtr) } nsresult -nsHttpProtocolConnection::Init(nsIUrl* url, nsISupports* eventSink) +nsHttpProtocolConnection::Init(nsIUrl* url, nsISupports* eventSink, + nsHttpProtocolHandler* handler) { nsresult rv; + mHandler = handler; + NS_ADDREF(mHandler); + mUrl = url; NS_ADDREF(mUrl); @@ -126,6 +133,23 @@ nsHttpProtocolConnection::Resume(void) //////////////////////////////////////////////////////////////////////////////// // nsIProtocolConnection methods: +NS_IMETHODIMP +nsHttpProtocolConnection::Open(void) +{ + nsresult rv = NS_OK; + + const char* host; + rv = mUrl->GetHost(&host); + if (NS_FAILED(rv)) return rv; + + PRInt32 port; + rv = mUrl->GetPort(&port); + if (NS_FAILED(rv)) return rv; + + rv = mHandler->GetTransport(host, port, &mTransport); + return rv; +} + NS_IMETHODIMP nsHttpProtocolConnection::GetContentType(char* *contentType) { @@ -139,7 +163,7 @@ nsHttpProtocolConnection::GetInputStream(nsIInputStream* *result) { if (mState != CONNECTED) return NS_ERROR_NOT_CONNECTED; - return NS_ERROR_NOT_IMPLEMENTED; + return mTransport->OpenInputStream(result); } NS_IMETHODIMP @@ -147,17 +171,7 @@ nsHttpProtocolConnection::GetOutputStream(nsIOutputStream* *result) { if (mState != CONNECTED) return NS_ERROR_NOT_CONNECTED; - return NS_ERROR_NOT_IMPLEMENTED; -} - -NS_IMETHODIMP -nsHttpProtocolConnection::AsyncWrite(nsIInputStream* data, PRUint32 count, - nsresult (*callback)(void* closure, PRUint32 count), - void* closure) -{ - if (mState != CONNECTED) - return NS_ERROR_NOT_CONNECTED; - return NS_ERROR_NOT_IMPLEMENTED; + return mTransport->OpenOutputStream(result); } //////////////////////////////////////////////////////////////////////////////// @@ -186,13 +200,15 @@ nsHttpProtocolConnection::Get(void) { nsresult rv; // NS_WITH_SERVICE(nsISocketTransportService, sts, kSocketTransportServiceCID, &rv); +// if (NS_FAILED(rv)) return rv; // XXX temporary: NS_WITH_SERVICE(nsIFileTransportService, sts, kFileTransportServiceCID, &rv); + if (NS_FAILED(rv)) return rv; const char* path = "y:/temp/foo.html"; // rv = sts->AsyncWrite(); - nsITransport* trans; +// nsITransport* trans; // rv = sts->AsyncRead(path, context, appEventQueue, this, &trans); if (NS_FAILED(rv)) return rv; @@ -231,6 +247,7 @@ nsHttpProtocolConnection::OnStopBinding(nsISupports* context, nsresult aStatus, nsIString* aMsg) { + // XXX switch from POSTING to GETTING state here, etc. return NS_ERROR_NOT_IMPLEMENTED; } diff --git a/mozilla/netwerk/protocol/http/src/nsHttpProtocolConnection.h b/mozilla/netwerk/protocol/http/src/nsHttpProtocolConnection.h index a832c700810..267c67a30bd 100644 --- a/mozilla/netwerk/protocol/http/src/nsHttpProtocolConnection.h +++ b/mozilla/netwerk/protocol/http/src/nsHttpProtocolConnection.h @@ -25,6 +25,8 @@ class nsIConnectionGroup; class nsIHttpEventSink; class nsIString; +class nsITransport; +class nsHttpProtocolHandler; class nsHttpProtocolConnection : public nsIHttpProtocolConnection, public nsIStreamListener @@ -38,12 +40,10 @@ public: NS_IMETHOD Resume(void); // nsIProtocolConnection methods: + NS_IMETHOD Open(void); NS_IMETHOD GetContentType(char* *contentType); NS_IMETHOD GetInputStream(nsIInputStream* *result); NS_IMETHOD GetOutputStream(nsIOutputStream* *result); - NS_IMETHOD AsyncWrite(nsIInputStream* data, PRUint32 count, - nsresult (*callback)(void* closure, PRUint32 count), - void* closure); // nsIHttpProtocolConnection methods: NS_IMETHOD GetHeader(const char* header); @@ -69,7 +69,10 @@ public: nsHttpProtocolConnection(); virtual ~nsHttpProtocolConnection(); - nsresult Init(nsIUrl* url, nsISupports* eventSink); + nsresult Init(nsIUrl* url, nsISupports* eventSink, + nsHttpProtocolHandler* handler); + nsresult GetExistingTransport(const char* host, PRInt32 port, + nsITransport* *result); enum State { UNCONNECTED, @@ -78,9 +81,11 @@ public: }; protected: - nsIUrl* mUrl; - nsIHttpEventSink* mEventSink; - State mState; + nsHttpProtocolHandler* mHandler; + nsIUrl* mUrl; + nsIHttpEventSink* mEventSink; + State mState; + nsITransport* mTransport; }; #endif /* nsHttpProtocolConnection_h___ */ diff --git a/mozilla/netwerk/protocol/http/src/nsHttpProtocolHandler.cpp b/mozilla/netwerk/protocol/http/src/nsHttpProtocolHandler.cpp index 50e3f7a842b..b3c51b7f0b3 100644 --- a/mozilla/netwerk/protocol/http/src/nsHttpProtocolHandler.cpp +++ b/mozilla/netwerk/protocol/http/src/nsHttpProtocolHandler.cpp @@ -22,17 +22,32 @@ #include "nsCRT.h" #include "nsIComponentManager.h" #include "nsIServiceManager.h" +#include "nsISocketTransportService.h" +#include "nsITransport.h" -static NS_DEFINE_CID(kTypicalUrlCID, NS_TYPICALURL_CID); +static NS_DEFINE_CID(kSocketTransportServiceCID, NS_SOCKETTRANSPORTSERVICE_CID); +static NS_DEFINE_CID(kTypicalUrlCID, NS_TYPICALURL_CID); //////////////////////////////////////////////////////////////////////////////// nsHttpProtocolHandler::nsHttpProtocolHandler() + : mConnectionPool(nsnull) { + NS_INIT_REFCNT(); +} + +nsresult +nsHttpProtocolHandler::Init(void) +{ + mConnectionPool = new nsHashtable(); + if (mConnectionPool == nsnull) + return NS_ERROR_OUT_OF_MEMORY; + return NS_OK; } nsHttpProtocolHandler::~nsHttpProtocolHandler() { + if (mConnectionPool) delete mConnectionPool; } NS_IMPL_ISUPPORTS(nsHttpProtocolHandler, nsIProtocolHandler::GetIID()); @@ -43,9 +58,7 @@ NS_IMPL_ISUPPORTS(nsHttpProtocolHandler, nsIProtocolHandler::GetIID()); NS_IMETHODIMP nsHttpProtocolHandler::GetScheme(const char* *result) { - *result = nsCRT::strdup("http"); - if (*result == nsnull) - return NS_ERROR_OUT_OF_MEMORY; + *result = "http"; return NS_OK; } @@ -103,7 +116,7 @@ nsHttpProtocolHandler::NewConnection(nsIUrl* url, nsHttpProtocolConnection* connection = new nsHttpProtocolConnection(); if (connection == nsnull) return NS_ERROR_OUT_OF_MEMORY; - rv = connection->Init(url, eventSink); + rv = connection->Init(url, eventSink, this); if (NS_FAILED(rv)) { delete connection; return rv; @@ -114,3 +127,43 @@ nsHttpProtocolHandler::NewConnection(nsIUrl* url, } //////////////////////////////////////////////////////////////////////////////// + +nsresult +nsHttpProtocolHandler::GetTransport(const char* host, PRInt32 port, + nsITransport* *result) +{ + nsSocketTransportKey key(host, port); + nsITransport* trans = (nsITransport*)mConnectionPool->Get(&key); + if (trans) { + *result = trans; + return NS_OK; + } + + nsresult rv; + NS_WITH_SERVICE(nsISocketTransportService, sts, kSocketTransportServiceCID, &rv); + if (NS_FAILED(rv)) return rv; + + rv = sts->CreateTransport(host, port, &trans); + if (NS_FAILED(rv)) return rv; + + void* oldValue = mConnectionPool->Put(&key, trans); + NS_ASSERTION(oldValue == nsnull, "race?"); + NS_ADDREF(trans); // released in ReleaseTransport + + return rv; +} + +nsresult +nsHttpProtocolHandler::ReleaseTransport(const char* host, PRInt32 port, + nsITransport* trans) +{ + nsSocketTransportKey key(host, port); + nsITransport* value = (nsITransport*)mConnectionPool->Remove(&key); + if (value == nsnull) + return NS_ERROR_FAILURE; + NS_ASSERTION(trans == value, "mConnectionPool out of sync"); + return NS_OK; +} + +//////////////////////////////////////////////////////////////////////////////// + diff --git a/mozilla/netwerk/protocol/http/src/nsHttpProtocolHandler.h b/mozilla/netwerk/protocol/http/src/nsHttpProtocolHandler.h index e3e10326ea7..fc5889422da 100644 --- a/mozilla/netwerk/protocol/http/src/nsHttpProtocolHandler.h +++ b/mozilla/netwerk/protocol/http/src/nsHttpProtocolHandler.h @@ -21,6 +21,9 @@ #include "nsIProtocolHandler.h" +class nsITransport; +class nsHashtable; + // XXX regenerate: #define NS_HTTPPROTOCOLHANDLER_CID \ { /* 59321440-f125-11d2-9322-000000000000 */ \ @@ -52,8 +55,45 @@ public: nsHttpProtocolHandler(); virtual ~nsHttpProtocolHandler(); + nsresult Init(void); + nsresult GetTransport(const char* host, PRInt32 port, + nsITransport* *result); + nsresult ReleaseTransport(const char* host, PRInt32 port, + nsITransport* trans); + protected: - + nsHashtable* mConnectionPool; }; +//////////////////////////////////////////////////////////////////////////////// + +#include "nsHashtable.h" +#include "plstr.h" + +class nsSocketTransportKey : public nsCStringKey +{ +private: + PRInt32 mPort; + +public: + nsSocketTransportKey(const char* host, PRInt32 port) + : nsCStringKey(host), mPort(port) + { + } + + ~nsSocketTransportKey(void) { + } + + PRBool Equals(const nsHashKey *aKey) const { + return mPort == ((nsSocketTransportKey*)aKey)->mPort + && nsCStringKey::Equals(aKey); + } + + nsHashKey *Clone(void) const { + return new nsSocketTransportKey(mStr, mPort); + } +}; + +//////////////////////////////////////////////////////////////////////////////// + #endif /* nsHttpProtocolHandler_h___ */