Bug 783562 - blob: protocol wrong Content-Type header. r=sicking
authorAndrea Marchesini <amarchesini@mozilla.com>
Mon, 27 Aug 2012 19:34:30 -0400
changeset 105646 9c0e1448ff3be564613b1af42b8f31a7b1de8c59
parent 105645 3854823c96e22a1a14c0cc2ec2938f00fd40e3b8
child 105647 8eb3be6ada0e3d93571c96f87ceb4da3b96668bd
push id55
push usershu@rfrn.org
push dateThu, 30 Aug 2012 01:33:09 +0000
reviewerssicking
bugs783562
milestone18.0a1
Bug 783562 - blob: protocol wrong Content-Type header. r=sicking
content/base/src/nsXMLHttpRequest.cpp
modules/libjar/nsJARChannel.cpp
modules/libjar/nsJARChannel.h
--- a/content/base/src/nsXMLHttpRequest.cpp
+++ b/content/base/src/nsXMLHttpRequest.cpp
@@ -72,16 +72,17 @@
 #include "mozilla/Telemetry.h"
 #include "jsfriendapi.h"
 #include "sampler.h"
 #include "mozilla/dom/XMLHttpRequestBinding.h"
 #include "nsIDOMFormData.h"
 #include "DictionaryHelpers.h"
 #include "mozilla/Attributes.h"
 #include "nsIPermissionManager.h"
+#include "nsMimeTypes.h"
 
 #include "nsWrapperCacheInlines.h"
 #include "nsStreamListenerWrapper.h"
 
 using namespace mozilla;
 using namespace mozilla::dom;
 
 #define LOAD_STR "load"
@@ -3082,19 +3083,25 @@ nsXMLHttpRequest::Send(nsIVariant* aVari
   // otherwise block us waiting for exclusive access to the cache.  If we don't
   // do this, then we could dead lock in some cases (see bug 309424).
   else if (!(mState & XML_HTTP_REQUEST_ASYNC)) {
     AddLoadFlags(mChannel,
         nsICachingChannel::LOAD_BYPASS_LOCAL_CACHE_IF_BUSY);
   }
 
   // Since we expect XML data, set the type hint accordingly
+  // if the channel doesn't know any content type.
   // This means that we always try to parse local files as XML
   // ignoring return value, as this is not critical
-  mChannel->SetContentType(NS_LITERAL_CSTRING("application/xml"));
+  nsCAutoString contentType;
+  if (NS_FAILED(mChannel->GetContentType(contentType)) ||
+      contentType.IsEmpty() ||
+      contentType.Equals(UNKNOWN_CONTENT_TYPE)) {
+    mChannel->SetContentType(NS_LITERAL_CSTRING("application/xml"));
+  }
 
   // We're about to send the request.  Start our timeout.
   mRequestSentTime = PR_Now();
   StartTimeoutTimer();
 
   // Set up the preflight if needed
   if (mState & XML_HTTP_REQUEST_NEED_AC_PREFLIGHT) {
     // Check to see if this initial OPTIONS request has already been cached
--- a/modules/libjar/nsJARChannel.cpp
+++ b/modules/libjar/nsJARChannel.cpp
@@ -184,17 +184,18 @@ nsJARInputThunk::IsNonBlocking(bool *non
 }
 
 //-----------------------------------------------------------------------------
 // nsJARChannel
 //-----------------------------------------------------------------------------
 
 
 nsJARChannel::nsJARChannel()
-    : mAppURI(nullptr)
+    : mOpened(false)
+    , mAppURI(nullptr)
     , mContentLength(-1)
     , mLoadFlags(LOAD_NORMAL)
     , mStatus(NS_OK)
     , mIsPending(false)
     , mIsUnsafe(true)
     , mJarInput(nullptr)
 {
 #if defined(PR_LOGGING)
@@ -576,17 +577,25 @@ nsJARChannel::GetSecurityInfo(nsISupport
     NS_PRECONDITION(aSecurityInfo, "Null out param");
     NS_IF_ADDREF(*aSecurityInfo = mSecurityInfo);
     return NS_OK;
 }
 
 NS_IMETHODIMP
 nsJARChannel::GetContentType(nsACString &result)
 {
+    // If the Jar file has not been open yet,
+    // We return application/x-unknown-content-type
+    if (!mOpened) {
+      result.Assign(UNKNOWN_CONTENT_TYPE);
+      return NS_OK;
+    }
+
     if (mContentType.IsEmpty()) {
+
         //
         // generate content type and set it
         //
         const char *ext = nullptr, *fileName = mJarEntry.get();
         int32_t len = mJarEntry.Length();
 
         // check if we're displaying a directory
         // mJarEntry will be empty if we're trying to display
@@ -705,16 +714,18 @@ nsJARChannel::Open(nsIInputStream **stre
         return NS_ERROR_UNEXPECTED;
 
     // force load the jar file now so GetContentLength will return a
     // meaningful value once we return.
     rv = mJarInput->EnsureJarStream();
     if (NS_FAILED(rv)) return rv;
 
     NS_ADDREF(*stream = mJarInput);
+
+    mOpened = true;
     return NS_OK;
 }
 
 NS_IMETHODIMP
 nsJARChannel::AsyncOpen(nsIStreamListener *listener, nsISupports *ctx)
 {
     LOG(("nsJARChannel::AsyncOpen [this=%x]\n", this));
 
@@ -749,16 +760,17 @@ nsJARChannel::AsyncOpen(nsIStreamListene
             mListener = nullptr;
             return rv;
         }
     }
 
     if (mLoadGroup)
         mLoadGroup->AddRequest(this, nullptr);
 
+    mOpened = true;
     return NS_OK;
 }
 
 //-----------------------------------------------------------------------------
 // nsIJARChannel
 //-----------------------------------------------------------------------------
 NS_IMETHODIMP
 nsJARChannel::GetIsUnsafe(bool *isUnsafe)
--- a/modules/libjar/nsJARChannel.h
+++ b/modules/libjar/nsJARChannel.h
@@ -48,16 +48,18 @@ public:
 private:
     nsresult CreateJarInput(nsIZipReaderCache *);
     nsresult EnsureJarInput(bool blocking);
 
 #if defined(PR_LOGGING)
     nsCString                       mSpec;
 #endif
 
+    bool                            mOpened;
+
     nsCOMPtr<nsIJARURI>             mJarURI;
     nsCOMPtr<nsIURI>                mOriginalURI;
     nsCOMPtr<nsIURI>                mAppURI;
     nsCOMPtr<nsISupports>           mOwner;
     nsCOMPtr<nsIInterfaceRequestor> mCallbacks;
     nsCOMPtr<nsISupports>           mSecurityInfo;
     nsCOMPtr<nsIProgressEventSink>  mProgressSink;
     nsCOMPtr<nsILoadGroup>          mLoadGroup;