From 645c88322d610c7588e27dc887b6dc26e3117f81 Mon Sep 17 00:00:00 2001 From: "jst%netscape.com" Date: Tue, 8 Jan 2002 06:32:36 +0000 Subject: [PATCH] Fixing bug 64248. Making the loading of external scripts pass the referrer to the server when requesting the script. r=gagan@netscape.com, sr=darin@netscape.com --- content/base/src/nsScriptLoader.cpp | 17 ++++++++++++++--- netwerk/base/public/nsIStreamLoader.idl | 4 +++- netwerk/base/public/nsNetUtil.h | 7 +++++-- netwerk/base/src/nsStreamLoader.cpp | 24 +++++++++++++++++++++--- 4 files changed, 43 insertions(+), 9 deletions(-) diff --git a/content/base/src/nsScriptLoader.cpp b/content/base/src/nsScriptLoader.cpp index 14ccd61dd6e..0808104d890 100644 --- a/content/base/src/nsScriptLoader.cpp +++ b/content/base/src/nsScriptLoader.cpp @@ -43,6 +43,7 @@ #include "nsIScriptElement.h" #include "nsIDocShell.h" #include "jsapi.h" +#include "nsIHttpChannel.h" static NS_DEFINE_CID(kCharsetConverterManagerCID, NS_ICHARSETCONVERTERMANAGER_CID); @@ -93,7 +94,12 @@ nsScriptLoadRequest::~nsScriptLoadRequest() { } -NS_IMPL_ISUPPORTS0(nsScriptLoadRequest) + +// The nsScriptLoadRequest is passed as the context to necko, and thus +// it needs to be threadsafe. Necko won't do anything with this +// context, but it will AddRef and Release it on other threads. + +NS_IMPL_THREADSAFE_ISUPPORTS0(nsScriptLoadRequest) void nsScriptLoadRequest::FireScriptAvailable(nsresult aResult, @@ -389,17 +395,22 @@ nsScriptLoader::ProcessScriptElement(nsIDOMHTMLScriptElement *aElement, return FireErrorNotification(rv, aElement, aObserver); } + // Get the referrer url from the document + nsCOMPtr documentURI; + mDocument->GetDocumentURL(getter_AddRefs(documentURI)); + nsCOMPtr prompter(do_QueryInterface(docshell)); + nsCOMPtr channel; rv = NS_NewStreamLoader(getter_AddRefs(loader), scriptURI, this, reqsup, loadGroup, prompter, - nsIChannel::LOAD_NORMAL); + nsIChannel::LOAD_NORMAL, documentURI, + nsIHttpChannel::REFERRER_INLINES); if (NS_FAILED(rv)) { mPendingRequests.RemoveElement(reqsup, 0); return FireErrorNotification(rv, aElement, aObserver); } } - } else { request->mLoading = PR_FALSE; request->mIsInline = PR_TRUE; diff --git a/netwerk/base/public/nsIStreamLoader.idl b/netwerk/base/public/nsIStreamLoader.idl index fcda649433d..907a73504cf 100644 --- a/netwerk/base/public/nsIStreamLoader.idl +++ b/netwerk/base/public/nsIStreamLoader.idl @@ -62,7 +62,9 @@ interface nsIStreamLoader : nsISupports in nsISupports ctxt, in nsILoadGroup loadGroup, in nsIInterfaceRequestor notificationCallbacks, - in nsLoadFlags loadAttributes); + in nsLoadFlags loadAttributes, + in nsIURI referrer, + in unsigned long refererFlags); /** * Gets the number of bytes read so far. diff --git a/netwerk/base/public/nsNetUtil.h b/netwerk/base/public/nsNetUtil.h index 6ff1eda4e37..62bd88c87d5 100644 --- a/netwerk/base/public/nsNetUtil.h +++ b/netwerk/base/public/nsNetUtil.h @@ -379,7 +379,9 @@ NS_NewStreamLoader(nsIStreamLoader* *result, nsISupports* context = nsnull, nsILoadGroup* loadGroup = nsnull, nsIInterfaceRequestor* notificationCallbacks = nsnull, - nsLoadFlags loadAttributes = NS_STATIC_CAST(nsLoadFlags, nsIRequest::LOAD_NORMAL)) + nsLoadFlags loadAttributes = NS_STATIC_CAST(nsLoadFlags, nsIRequest::LOAD_NORMAL), + nsIURI* referrer = nsnull, + PRUint32 referrerFlags = 0) { nsresult rv; nsCOMPtr loader; @@ -390,7 +392,8 @@ NS_NewStreamLoader(nsIStreamLoader* *result, getter_AddRefs(loader)); if (NS_FAILED(rv)) return rv; rv = loader->Init(uri, observer, context, loadGroup, - notificationCallbacks, loadAttributes); + notificationCallbacks, loadAttributes, referrer, + referrerFlags); if (NS_FAILED(rv)) return rv; *result = loader; diff --git a/netwerk/base/src/nsStreamLoader.cpp b/netwerk/base/src/nsStreamLoader.cpp index bd3dfe250a6..c9cbc6d62bc 100644 --- a/netwerk/base/src/nsStreamLoader.cpp +++ b/netwerk/base/src/nsStreamLoader.cpp @@ -40,6 +40,7 @@ #include "nsIURL.h" #include "nsNetUtil.h" #include "nsIChannel.h" +#include "nsIHttpChannel.h" #include "nsProxiedService.h" static NS_DEFINE_CID(kProxyObjectManagerCID, NS_PROXYEVENT_MANAGER_CID); @@ -50,12 +51,29 @@ nsStreamLoader::Init(nsIURI* aURL, nsISupports* context, nsILoadGroup* aGroup, nsIInterfaceRequestor* notificationCallbacks, - nsLoadFlags loadAttributes) + nsLoadFlags loadAttributes, + nsIURI *referrer, + PRUint32 referrerFlags) { nsresult rv = NS_OK; - rv = NS_OpenURI(this, nsnull, aURL, nsnull, aGroup, notificationCallbacks, - loadAttributes); + nsCOMPtr channel; + + rv = NS_OpenURI(getter_AddRefs(channel), aURL, nsnull, aGroup, + notificationCallbacks, loadAttributes); + if (NS_FAILED(rv)) return rv; + + if (referrer) { + nsCOMPtr httpChannel(do_QueryInterface(channel)); + + if (httpChannel) { + rv = httpChannel->SetReferrer(referrer, referrerFlags); + if (NS_FAILED(rv)) return rv; + } + } + + rv = channel->AsyncOpen(this, context); + if (NS_FAILED(rv) && observer) { // don't callback synchronously as it puts the caller // in a recursive situation and breaks the asynchronous