From 0b10eee40b6af052a9a79e59c9d09b97d49cd5cd Mon Sep 17 00:00:00 2001 From: Jason Duell Date: Thu, 19 Feb 2009 15:25:18 -0500 Subject: [PATCH] Bug 474536. Expose the underlying channel's content-disposition on jar: channels. r+sr=bzbarsky --- modules/libjar/nsJARChannel.cpp | 35 +++++++++++++++------- modules/libjar/nsJARChannel.h | 2 ++ netwerk/base/public/nsChannelProperties.h | 10 +++++++ netwerk/base/public/nsNetStrings.h | 1 + netwerk/base/public/nsNetUtil.h | 14 +++++++++ netwerk/base/src/nsNetStrings.cpp | 1 + netwerk/protocol/http/src/nsHttpAtomList.h | 1 + netwerk/streamconv/test/Makefile.in | 1 - uriloader/base/nsURILoader.cpp | 5 +++- 9 files changed, 58 insertions(+), 12 deletions(-) diff --git a/modules/libjar/nsJARChannel.cpp b/modules/libjar/nsJARChannel.cpp index 20587270716..73914dfae09 100644 --- a/modules/libjar/nsJARChannel.cpp +++ b/modules/libjar/nsJARChannel.cpp @@ -46,6 +46,7 @@ #include "nsEscape.h" #include "nsIPrefService.h" #include "nsIPrefBranch.h" +#include "nsChannelProperties.h" #include "nsIScriptSecurityManager.h" #include "nsIPrincipal.h" @@ -218,6 +219,9 @@ nsJARInputThunk::IsNonBlocking(PRBool *nonBlocking) } //----------------------------------------------------------------------------- +// nsJARChannel +//----------------------------------------------------------------------------- + nsJARChannel::nsJARChannel() : mContentLength(-1) @@ -246,18 +250,23 @@ nsJARChannel::~nsJARChannel() NS_RELEASE(handler); // NULL parameter } -NS_IMPL_ISUPPORTS6(nsJARChannel, - nsIRequest, - nsIChannel, - nsIStreamListener, - nsIRequestObserver, - nsIDownloadObserver, - nsIJARChannel) +NS_IMPL_ISUPPORTS_INHERITED6(nsJARChannel, + nsHashPropertyBag, + nsIRequest, + nsIChannel, + nsIStreamListener, + nsIRequestObserver, + nsIDownloadObserver, + nsIJARChannel) nsresult nsJARChannel::Init(nsIURI *uri) { nsresult rv; + rv = nsHashPropertyBag::Init(); + if (NS_FAILED(rv)) + return rv; + mJarURI = do_QueryInterface(uri, &rv); if (NS_FAILED(rv)) return rv; @@ -752,6 +761,7 @@ nsJARChannel::OnDownloadComplete(nsIDownloader *downloader, } if (NS_SUCCEEDED(status) && channel) { + nsCAutoString header; // Grab the security info from our base channel channel->GetSecurityInfo(getter_AddRefs(mSecurityInfo)); @@ -760,16 +770,17 @@ nsJARChannel::OnDownloadComplete(nsIDownloader *downloader, // We only want to run scripts if the server really intended to // send us a JAR file. Check the server-supplied content type for // a JAR type. - nsCAutoString header; httpChannel->GetResponseHeader(NS_LITERAL_CSTRING("Content-Type"), header); - nsCAutoString contentType; nsCAutoString charset; NS_ParseContentType(header, contentType, charset); - mIsUnsafe = !contentType.EqualsLiteral("application/java-archive") && !contentType.EqualsLiteral("application/x-jar"); + rv = httpChannel->GetResponseHeader(NS_LITERAL_CSTRING("Content-Disposition"), + header); + if (NS_SUCCEEDED(rv)) + SetPropertyAsACString(NS_CHANNEL_PROP_CONTENT_DISPOSITION, header); } else { nsCOMPtr innerJARChannel(do_QueryInterface(channel)); if (innerJARChannel) { @@ -777,6 +788,10 @@ nsJARChannel::OnDownloadComplete(nsIDownloader *downloader, innerJARChannel->GetIsUnsafe(&unsafe); mIsUnsafe = unsafe; } + // Soon-to-be common way to get Disposition: right now only nsIJARChannel + rv = NS_GetContentDisposition(request, header); + if (NS_SUCCEEDED(rv)) + SetPropertyAsACString(NS_CHANNEL_PROP_CONTENT_DISPOSITION, header); } } diff --git a/modules/libjar/nsJARChannel.h b/modules/libjar/nsJARChannel.h index 719b298099b..9e84f58e9c7 100644 --- a/modules/libjar/nsJARChannel.h +++ b/modules/libjar/nsJARChannel.h @@ -47,6 +47,7 @@ #include "nsIZipReader.h" #include "nsIDownloader.h" #include "nsILoadGroup.h" +#include "nsHashPropertyBag.h" #include "nsIFile.h" #include "nsIURI.h" #include "nsCOMPtr.h" @@ -60,6 +61,7 @@ class nsJARInputThunk; class nsJARChannel : public nsIJARChannel , public nsIDownloadObserver , public nsIStreamListener + , public nsHashPropertyBag { public: NS_DECL_ISUPPORTS diff --git a/netwerk/base/public/nsChannelProperties.h b/netwerk/base/public/nsChannelProperties.h index 5a458215b52..f1036a6f4c3 100644 --- a/netwerk/base/public/nsChannelProperties.h +++ b/netwerk/base/public/nsChannelProperties.h @@ -58,11 +58,21 @@ */ #define NS_CHANNEL_PROP_CONTENT_LENGTH_STR "content-length" +/** + * MIME Content-Disposition header of channel. + * Not available before onStartRequest. + * Type: nsACString + */ +#define NS_CHANNEL_PROP_CONTENT_DISPOSITION_STR "content-disposition" + #ifdef IMPL_NS_NET #define NS_CHANNEL_PROP_CONTENT_LENGTH gNetStrings->kContentLength +#define NS_CHANNEL_PROP_CONTENT_DISPOSITION gNetStrings->kContentDisposition #else #define NS_CHANNEL_PROP_CONTENT_LENGTH \ NS_LITERAL_STRING(NS_CHANNEL_PROP_CONTENT_LENGTH_STR) +#define NS_CHANNEL_PROP_CONTENT_DISPOSITION \ + NS_LITERAL_STRING(NS_CHANNEL_PROP_CONTENT_DISPOSITION_STR) #endif #endif diff --git a/netwerk/base/public/nsNetStrings.h b/netwerk/base/public/nsNetStrings.h index 603457a14d7..44d63a77e8e 100644 --- a/netwerk/base/public/nsNetStrings.h +++ b/netwerk/base/public/nsNetStrings.h @@ -49,6 +49,7 @@ public: /** "content-length" */ const nsLiteralString kContentLength; + const nsLiteralString kContentDisposition; }; extern NS_HIDDEN_(nsNetStrings*) gNetStrings; diff --git a/netwerk/base/public/nsNetUtil.h b/netwerk/base/public/nsNetUtil.h index 5db226153cb..05eb50cb549 100644 --- a/netwerk/base/public/nsNetUtil.h +++ b/netwerk/base/public/nsNetUtil.h @@ -62,6 +62,7 @@ #include "nsIIOService.h" #include "nsIServiceManager.h" #include "nsIChannel.h" +#include "nsChannelProperties.h" #include "nsIInputStreamChannel.h" #include "nsITransport.h" #include "nsIStreamTransportService.h" @@ -194,6 +195,19 @@ NS_NewChannel(nsIChannel **result, return rv; } +// For now, works only with JARChannel. Future: with all channels that may +// have Content-Disposition header (JAR, nsIHttpChannel, and nsIMultiPartChannel). +inline nsresult +NS_GetContentDisposition(nsIRequest *channel, + nsACString &result) +{ + nsCOMPtr props(do_QueryInterface(channel)); + if (props) + return props->GetPropertyAsACString(NS_CHANNEL_PROP_CONTENT_DISPOSITION, + result); + return NS_ERROR_NOT_AVAILABLE; +} + // Use this function with CAUTION. It creates a stream that blocks when you // Read() from it and blocking the UI thread is a bad idea. If you don't want // to implement a full blown asynchronous consumer (via nsIStreamListener) look diff --git a/netwerk/base/src/nsNetStrings.cpp b/netwerk/base/src/nsNetStrings.cpp index b8d47d8f9fd..2ac1a864ac5 100644 --- a/netwerk/base/src/nsNetStrings.cpp +++ b/netwerk/base/src/nsNetStrings.cpp @@ -41,6 +41,7 @@ NS_HIDDEN_(nsNetStrings*) gNetStrings; nsNetStrings::nsNetStrings() : NS_LITERAL_STRING_INIT(kContentLength, NS_CHANNEL_PROP_CONTENT_LENGTH_STR) + : NS_LITERAL_STRING_INIT(kContentDisposition, NS_CHANNEL_PROP_CONTENT_DISPOSITION_STR) {} diff --git a/netwerk/protocol/http/src/nsHttpAtomList.h b/netwerk/protocol/http/src/nsHttpAtomList.h index 77096dae78f..f11c724dc78 100644 --- a/netwerk/protocol/http/src/nsHttpAtomList.h +++ b/netwerk/protocol/http/src/nsHttpAtomList.h @@ -62,6 +62,7 @@ HTTP_ATOM(Authorization, "Authorization") HTTP_ATOM(Cache_Control, "Cache-Control") HTTP_ATOM(Connection, "Connection") HTTP_ATOM(Content_Base, "Content-Base") +HTTP_ATOM(Content_Disposition, "Content-Disposition") HTTP_ATOM(Content_Encoding, "Content-Encoding") HTTP_ATOM(Content_Language, "Content-Language") HTTP_ATOM(Content_Length, "Content-Length") diff --git a/netwerk/streamconv/test/Makefile.in b/netwerk/streamconv/test/Makefile.in index 4b8b5975891..ea2f55b490e 100644 --- a/netwerk/streamconv/test/Makefile.in +++ b/netwerk/streamconv/test/Makefile.in @@ -73,4 +73,3 @@ DEFINES += -DNGPREFS endif endif # WINNT -DEFINES += -DIMPL_NS_NET diff --git a/uriloader/base/nsURILoader.cpp b/uriloader/base/nsURILoader.cpp index 770bcaac25b..a5938777950 100644 --- a/uriloader/base/nsURILoader.cpp +++ b/uriloader/base/nsURILoader.cpp @@ -65,7 +65,7 @@ #include "nsXPIDLString.h" #include "nsString.h" - +#include "nsNetUtil.h" #include "nsIDOMWindowInternal.h" #include "nsReadableUtils.h" #include "nsDOMError.h" @@ -380,6 +380,9 @@ nsresult nsDocumentOpenInfo::DispatchContent(nsIRequest *request, nsISupports * if (multipartChannel) { rv = multipartChannel->GetContentDisposition(disposition); + } else { + // Soon-to-be common way to get Disposition: right now only JARChannel + rv = NS_GetContentDisposition(request, disposition); } }