From 2f16fd55a408b31e1145b1d4cf4683cd3294510e Mon Sep 17 00:00:00 2001 From: Paul Adenot Date: Tue, 26 Jun 2012 10:33:00 -0700 Subject: [PATCH] Bug 767087 - Add a flag to force content sniffing when we get served application/octet-stream. r=jduell --- netwerk/base/public/nsIChannel.idl | 8 ++++++++ netwerk/base/src/nsBaseChannel.cpp | 12 +++++++++--- netwerk/protocol/http/nsHttpChannel.cpp | 5 ++++- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/netwerk/base/public/nsIChannel.idl b/netwerk/base/public/nsIChannel.idl index 9947e597778..78e86b8d721 100644 --- a/netwerk/base/public/nsIChannel.idl +++ b/netwerk/base/public/nsIChannel.idl @@ -241,6 +241,14 @@ interface nsIChannel : nsIRequest */ const unsigned long LOAD_CLASSIFY_URI = 1 << 22; + /** + * If this flag is set and a server's response is Content-Type + * application/octet-steam, the server's Content-Type will be ignored and + * the channel content will be sniffed as though no Content-Type had been + * passed. + */ + const unsigned long LOAD_TREAT_APPLICATION_OCTET_STREAM_AS_UNKNOWN = 1 << 23; + /** * Access to the type implied or stated by the Content-Disposition header * if available and if applicable. This allows determining inline versus diff --git a/netwerk/base/src/nsBaseChannel.cpp b/netwerk/base/src/nsBaseChannel.cpp index 7a06a3ab0cc..6bd29ba0138 100644 --- a/netwerk/base/src/nsBaseChannel.cpp +++ b/netwerk/base/src/nsBaseChannel.cpp @@ -682,9 +682,15 @@ CallUnknownTypeSniffer(void *aClosure, const PRUint8 *aData, PRUint32 aCount) NS_IMETHODIMP nsBaseChannel::OnStartRequest(nsIRequest *request, nsISupports *ctxt) { - // If our content type is unknown, then use the content type sniffer. If the - // sniffer is not available for some reason, then we just keep going as-is. - if (NS_SUCCEEDED(mStatus) && mContentType.EqualsLiteral(UNKNOWN_CONTENT_TYPE)) { + // If our content type is unknown or if the content type is + // application/octet-stream and the caller requested it, use the content type + // sniffer. If the sniffer is not available for some reason, then we just keep + // going as-is. + bool shouldSniff = mContentType.EqualsLiteral(UNKNOWN_CONTENT_TYPE) || + ((mLoadFlags & LOAD_TREAT_APPLICATION_OCTET_STREAM_AS_UNKNOWN) && + mContentType.EqualsLiteral(APPLICATION_OCTET_STREAM)); + + if (NS_SUCCEEDED(mStatus) && shouldSniff) { mPump->PeekStream(CallUnknownTypeSniffer, static_cast(this)); } diff --git a/netwerk/protocol/http/nsHttpChannel.cpp b/netwerk/protocol/http/nsHttpChannel.cpp index 116d66d36c4..ea8db6a9644 100644 --- a/netwerk/protocol/http/nsHttpChannel.cpp +++ b/netwerk/protocol/http/nsHttpChannel.cpp @@ -881,8 +881,11 @@ nsresult nsHttpChannel::CallOnStartRequest() { mTracingEnabled = false; + bool shouldSniff = mResponseHead && (mResponseHead->ContentType().IsEmpty() || + ((mResponseHead->ContentType().EqualsLiteral(APPLICATION_OCTET_STREAM) && + (mLoadFlags & LOAD_TREAT_APPLICATION_OCTET_STREAM_AS_UNKNOWN)))); - if (mResponseHead && mResponseHead->ContentType().IsEmpty()) { + if (shouldSniff) { NS_ASSERTION(mConnectionInfo, "Should have connection info here"); if (!mContentTypeHint.IsEmpty()) mResponseHead->SetContentType(mContentTypeHint);