netwerk/streamconv/converters/nsUnknownDecoder.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/netwerk/streamconv/converters/nsUnknownDecoder.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,639 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include "nsUnknownDecoder.h"
    1.10 +#include "nsIPipe.h"
    1.11 +#include "nsIInputStream.h"
    1.12 +#include "nsIOutputStream.h"
    1.13 +#include "nsMimeTypes.h"
    1.14 +#include "nsIPrefService.h"
    1.15 +#include "nsIPrefBranch.h"
    1.16 +
    1.17 +#include "nsCRT.h"
    1.18 +
    1.19 +#include "nsIMIMEService.h"
    1.20 +
    1.21 +#include "nsIViewSourceChannel.h"
    1.22 +#include "nsIHttpChannel.h"
    1.23 +#include "nsNetCID.h"
    1.24 +#include "nsNetUtil.h"
    1.25 +
    1.26 +
    1.27 +#define MAX_BUFFER_SIZE 512
    1.28 +
    1.29 +nsUnknownDecoder::nsUnknownDecoder()
    1.30 +  : mBuffer(nullptr)
    1.31 +  , mBufferLen(0)
    1.32 +  , mRequireHTMLsuffix(false)
    1.33 +{
    1.34 +  nsCOMPtr<nsIPrefBranch> prefs = do_GetService(NS_PREFSERVICE_CONTRACTID);
    1.35 +  if (prefs) {
    1.36 +    bool val;
    1.37 +    if (NS_SUCCEEDED(prefs->GetBoolPref("security.requireHTMLsuffix", &val)))
    1.38 +      mRequireHTMLsuffix = val;
    1.39 +  }
    1.40 +}
    1.41 +
    1.42 +nsUnknownDecoder::~nsUnknownDecoder()
    1.43 +{
    1.44 +  if (mBuffer) {
    1.45 +    delete [] mBuffer;
    1.46 +    mBuffer = nullptr;
    1.47 +  }
    1.48 +}
    1.49 +
    1.50 +// ----
    1.51 +//
    1.52 +// nsISupports implementation...
    1.53 +//
    1.54 +// ----
    1.55 +
    1.56 +NS_IMPL_ADDREF(nsUnknownDecoder)
    1.57 +NS_IMPL_RELEASE(nsUnknownDecoder)
    1.58 +
    1.59 +NS_INTERFACE_MAP_BEGIN(nsUnknownDecoder)
    1.60 +   NS_INTERFACE_MAP_ENTRY(nsIStreamConverter)
    1.61 +   NS_INTERFACE_MAP_ENTRY(nsIStreamListener)
    1.62 +   NS_INTERFACE_MAP_ENTRY(nsIRequestObserver)
    1.63 +   NS_INTERFACE_MAP_ENTRY(nsIContentSniffer)
    1.64 +   NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIStreamListener)
    1.65 +NS_INTERFACE_MAP_END
    1.66 +
    1.67 +
    1.68 +// ----
    1.69 +//
    1.70 +// nsIStreamConverter methods...
    1.71 +//
    1.72 +// ----
    1.73 +
    1.74 +NS_IMETHODIMP
    1.75 +nsUnknownDecoder::Convert(nsIInputStream *aFromStream,
    1.76 +                          const char *aFromType,
    1.77 +                          const char *aToType,
    1.78 +                          nsISupports *aCtxt, 
    1.79 +                          nsIInputStream **aResultStream) 
    1.80 +{
    1.81 +  return NS_ERROR_NOT_IMPLEMENTED;
    1.82 +}
    1.83 +
    1.84 +NS_IMETHODIMP
    1.85 +nsUnknownDecoder::AsyncConvertData(const char *aFromType, 
    1.86 +                                   const char *aToType,
    1.87 +                                   nsIStreamListener *aListener, 
    1.88 +                                   nsISupports *aCtxt)
    1.89 +{
    1.90 +  NS_ASSERTION(aListener && aFromType && aToType, 
    1.91 +               "null pointer passed into multi mixed converter");
    1.92 +  // hook up our final listener. this guy gets the various On*() calls we want to throw
    1.93 +  // at him.
    1.94 +  //
    1.95 +  mNextListener = aListener;
    1.96 +  return (aListener) ? NS_OK : NS_ERROR_FAILURE;
    1.97 +}
    1.98 +
    1.99 +// ----
   1.100 +//
   1.101 +// nsIStreamListener methods...
   1.102 +//
   1.103 +// ----
   1.104 +
   1.105 +NS_IMETHODIMP
   1.106 +nsUnknownDecoder::OnDataAvailable(nsIRequest* request, 
   1.107 +                                  nsISupports *aCtxt,
   1.108 +                                  nsIInputStream *aStream, 
   1.109 +                                  uint64_t aSourceOffset, 
   1.110 +                                  uint32_t aCount)
   1.111 +{
   1.112 +  nsresult rv = NS_OK;
   1.113 +
   1.114 +  if (!mNextListener) return NS_ERROR_FAILURE;
   1.115 +
   1.116 +  if (mContentType.IsEmpty()) {
   1.117 +    uint32_t count, len;
   1.118 +
   1.119 +    // If the buffer has not been allocated by now, just fail...
   1.120 +    if (!mBuffer) return NS_ERROR_OUT_OF_MEMORY;
   1.121 +
   1.122 +    //
   1.123 +    // Determine how much of the stream should be read to fill up the
   1.124 +    // sniffer buffer...
   1.125 +    //
   1.126 +    if (mBufferLen + aCount >= MAX_BUFFER_SIZE) {
   1.127 +      count = MAX_BUFFER_SIZE-mBufferLen;
   1.128 +    } else {
   1.129 +      count = aCount;
   1.130 +    }
   1.131 +
   1.132 +    // Read the data into the buffer...
   1.133 +    rv = aStream->Read((mBuffer+mBufferLen), count, &len);
   1.134 +    if (NS_FAILED(rv)) return rv;
   1.135 +
   1.136 +    mBufferLen += len;
   1.137 +    aCount     -= len;
   1.138 +
   1.139 +    if (aCount) {
   1.140 +      //
   1.141 +      // Adjust the source offset...  The call to FireListenerNotifications(...)
   1.142 +      // will make the first OnDataAvailable(...) call with an offset of 0.
   1.143 +      // So, this offset needs to be adjusted to reflect that...
   1.144 +      //
   1.145 +      aSourceOffset += mBufferLen;
   1.146 +
   1.147 +      DetermineContentType(request);
   1.148 +
   1.149 +      rv = FireListenerNotifications(request, aCtxt);
   1.150 +    }
   1.151 +  }
   1.152 +
   1.153 +  // Must not fire ODA again if it failed once
   1.154 +  if (aCount && NS_SUCCEEDED(rv)) {
   1.155 +    NS_ASSERTION(!mContentType.IsEmpty(), 
   1.156 +                 "Content type should be known by now.");
   1.157 +
   1.158 +    rv = mNextListener->OnDataAvailable(request, aCtxt, aStream, 
   1.159 +                                        aSourceOffset, aCount);
   1.160 +  }
   1.161 +
   1.162 +  return rv;
   1.163 +}
   1.164 +
   1.165 +// ----
   1.166 +//
   1.167 +// nsIRequestObserver methods...
   1.168 +//
   1.169 +// ----
   1.170 +
   1.171 +NS_IMETHODIMP
   1.172 +nsUnknownDecoder::OnStartRequest(nsIRequest* request, nsISupports *aCtxt) 
   1.173 +{
   1.174 +  nsresult rv = NS_OK;
   1.175 +
   1.176 +  if (!mNextListener) return NS_ERROR_FAILURE;
   1.177 +
   1.178 +  // Allocate the sniffer buffer...
   1.179 +  if (NS_SUCCEEDED(rv) && !mBuffer) {
   1.180 +    mBuffer = new char[MAX_BUFFER_SIZE];
   1.181 +
   1.182 +    if (!mBuffer) {
   1.183 +      rv = NS_ERROR_OUT_OF_MEMORY;
   1.184 +    }
   1.185 +  }
   1.186 +
   1.187 +  // Do not pass the OnStartRequest on to the next listener (yet)...
   1.188 +  return rv;
   1.189 +}
   1.190 +
   1.191 +NS_IMETHODIMP
   1.192 +nsUnknownDecoder::OnStopRequest(nsIRequest* request, nsISupports *aCtxt,
   1.193 +                                nsresult aStatus)
   1.194 +{
   1.195 +  nsresult rv = NS_OK;
   1.196 +
   1.197 +  if (!mNextListener) return NS_ERROR_FAILURE;
   1.198 +
   1.199 +  //
   1.200 +  // The total amount of data is less than the size of the sniffer buffer.
   1.201 +  // Analyze the buffer now...
   1.202 +  //
   1.203 +  if (mContentType.IsEmpty()) {
   1.204 +    DetermineContentType(request);
   1.205 +
   1.206 +    rv = FireListenerNotifications(request, aCtxt);
   1.207 +
   1.208 +    if (NS_FAILED(rv)) {
   1.209 +      aStatus = rv;
   1.210 +    }
   1.211 +  }
   1.212 +
   1.213 +  rv = mNextListener->OnStopRequest(request, aCtxt, aStatus);
   1.214 +  mNextListener = 0;
   1.215 +
   1.216 +  return rv;
   1.217 +}
   1.218 +
   1.219 +// ----
   1.220 +//
   1.221 +// nsIContentSniffer methods...
   1.222 +//
   1.223 +// ----
   1.224 +NS_IMETHODIMP
   1.225 +nsUnknownDecoder::GetMIMETypeFromContent(nsIRequest* aRequest,
   1.226 +                                         const uint8_t* aData,
   1.227 +                                         uint32_t aLength,
   1.228 +                                         nsACString& type)
   1.229 +{
   1.230 +  mBuffer = const_cast<char*>(reinterpret_cast<const char*>(aData));
   1.231 +  mBufferLen = aLength;
   1.232 +  DetermineContentType(aRequest);
   1.233 +  mBuffer = nullptr;
   1.234 +  mBufferLen = 0;
   1.235 +  type.Assign(mContentType);
   1.236 +  mContentType.Truncate();
   1.237 +  return type.IsEmpty() ? NS_ERROR_NOT_AVAILABLE : NS_OK;
   1.238 +}
   1.239 +
   1.240 +
   1.241 +// Actual sniffing code
   1.242 +
   1.243 +bool nsUnknownDecoder::AllowSniffing(nsIRequest* aRequest)
   1.244 +{
   1.245 +  if (!mRequireHTMLsuffix) {
   1.246 +    return true;
   1.247 +  }
   1.248 +  
   1.249 +  nsCOMPtr<nsIChannel> channel = do_QueryInterface(aRequest);
   1.250 +  if (!channel) {
   1.251 +    NS_ERROR("QI failed");
   1.252 +    return false;
   1.253 +  }
   1.254 +
   1.255 +  nsCOMPtr<nsIURI> uri;
   1.256 +  if (NS_FAILED(channel->GetURI(getter_AddRefs(uri))) || !uri) {
   1.257 +    return false;
   1.258 +  }
   1.259 +  
   1.260 +  bool isLocalFile = false;
   1.261 +  if (NS_FAILED(uri->SchemeIs("file", &isLocalFile)) || isLocalFile) {
   1.262 +    return false;
   1.263 +  }
   1.264 +
   1.265 +  return true;
   1.266 +}
   1.267 +
   1.268 +/**
   1.269 + * This is the array of sniffer entries that depend on "magic numbers"
   1.270 + * in the file.  Each entry has either a type associated with it (set
   1.271 + * these with the SNIFFER_ENTRY macro) or a function to be executed
   1.272 + * (set these with the SNIFFER_ENTRY_WITH_FUNC macro).  The function
   1.273 + * should take a single nsIRequest* and returns bool -- true if
   1.274 + * it sets mContentType, false otherwise
   1.275 + */
   1.276 +nsUnknownDecoder::nsSnifferEntry nsUnknownDecoder::sSnifferEntries[] = {
   1.277 +  SNIFFER_ENTRY("%PDF-", APPLICATION_PDF),
   1.278 +
   1.279 +  SNIFFER_ENTRY("%!PS-Adobe-", APPLICATION_POSTSCRIPT),
   1.280 +
   1.281 +  // Files that start with mailbox delimiters let's provisionally call
   1.282 +  // text/plain
   1.283 +  SNIFFER_ENTRY("From", TEXT_PLAIN),
   1.284 +  SNIFFER_ENTRY(">From", TEXT_PLAIN),
   1.285 +
   1.286 +  // If the buffer begins with "#!" or "%!" then it is a script of
   1.287 +  // some sort...  "Scripts" can include arbitrary data to be passed
   1.288 +  // to an interpreter, so we need to decide whether we can call this
   1.289 +  // text or whether it's data.
   1.290 +  SNIFFER_ENTRY_WITH_FUNC("#!", &nsUnknownDecoder::LastDitchSniff),
   1.291 +
   1.292 +  // XXXbz should (and can) we also include the various ways that <?xml can
   1.293 +  // appear as UTF-16 and such?  See http://www.w3.org/TR/REC-xml#sec-guessing
   1.294 +  SNIFFER_ENTRY_WITH_FUNC("<?xml", &nsUnknownDecoder::SniffForXML)
   1.295 +};
   1.296 +
   1.297 +uint32_t nsUnknownDecoder::sSnifferEntryNum =
   1.298 +  sizeof(nsUnknownDecoder::sSnifferEntries) /
   1.299 +    sizeof(nsUnknownDecoder::nsSnifferEntry);
   1.300 +
   1.301 +void nsUnknownDecoder::DetermineContentType(nsIRequest* aRequest)
   1.302 +{
   1.303 +  NS_ASSERTION(mContentType.IsEmpty(), "Content type is already known.");
   1.304 +  if (!mContentType.IsEmpty()) return;
   1.305 +
   1.306 +  // First, run through all the types we can detect reliably based on
   1.307 +  // magic numbers
   1.308 +  uint32_t i;
   1.309 +  for (i = 0; i < sSnifferEntryNum; ++i) {
   1.310 +    if (mBufferLen >= sSnifferEntries[i].mByteLen &&  // enough data
   1.311 +        memcmp(mBuffer, sSnifferEntries[i].mBytes, sSnifferEntries[i].mByteLen) == 0) {  // and type matches
   1.312 +      NS_ASSERTION(sSnifferEntries[i].mMimeType ||
   1.313 +                   sSnifferEntries[i].mContentTypeSniffer,
   1.314 +                   "Must have either a type string or a function to set the type");
   1.315 +      NS_ASSERTION(!sSnifferEntries[i].mMimeType ||
   1.316 +                   !sSnifferEntries[i].mContentTypeSniffer,
   1.317 +                   "Both a type string and a type sniffing function set;"
   1.318 +                   " using type string");
   1.319 +      if (sSnifferEntries[i].mMimeType) {
   1.320 +        mContentType = sSnifferEntries[i].mMimeType;
   1.321 +        NS_ASSERTION(!mContentType.IsEmpty(), 
   1.322 +                     "Content type should be known by now.");
   1.323 +        return;
   1.324 +      }
   1.325 +      if ((this->*(sSnifferEntries[i].mContentTypeSniffer))(aRequest)) {
   1.326 +        NS_ASSERTION(!mContentType.IsEmpty(), 
   1.327 +                     "Content type should be known by now.");
   1.328 +        return;
   1.329 +      }        
   1.330 +    }
   1.331 +  }
   1.332 +
   1.333 +  NS_SniffContent(NS_DATA_SNIFFER_CATEGORY, aRequest,
   1.334 +                  (const uint8_t*)mBuffer, mBufferLen, mContentType);
   1.335 +  if (!mContentType.IsEmpty()) {
   1.336 +    return;
   1.337 +  }
   1.338 +
   1.339 +  if (SniffForHTML(aRequest)) {
   1.340 +    NS_ASSERTION(!mContentType.IsEmpty(), 
   1.341 +                 "Content type should be known by now.");
   1.342 +    return;
   1.343 +  }
   1.344 +  
   1.345 +  // We don't know what this is yet.  Before we just give up, try
   1.346 +  // the URI from the request.
   1.347 +  if (SniffURI(aRequest)) {
   1.348 +    NS_ASSERTION(!mContentType.IsEmpty(), 
   1.349 +                 "Content type should be known by now.");
   1.350 +    return;
   1.351 +  }
   1.352 +  
   1.353 +  LastDitchSniff(aRequest);
   1.354 +  NS_ASSERTION(!mContentType.IsEmpty(), 
   1.355 +               "Content type should be known by now.");
   1.356 +}
   1.357 +
   1.358 +bool nsUnknownDecoder::SniffForHTML(nsIRequest* aRequest)
   1.359 +{
   1.360 +  /*
   1.361 +   * To prevent a possible attack, we will not consider this to be
   1.362 +   * html content if it comes from the local file system and our prefs
   1.363 +   * are set right
   1.364 +   */
   1.365 +  if (!AllowSniffing(aRequest)) {
   1.366 +    return false;
   1.367 +  }
   1.368 +  
   1.369 +  // Now look for HTML.
   1.370 +  const char* str = mBuffer;
   1.371 +  const char* end = mBuffer + mBufferLen;
   1.372 +
   1.373 +  // skip leading whitespace
   1.374 +  while (str != end && nsCRT::IsAsciiSpace(*str)) {
   1.375 +    ++str;
   1.376 +  }
   1.377 +
   1.378 +  // did we find something like a start tag?
   1.379 +  if (str == end || *str != '<' || ++str == end) {
   1.380 +    return false;
   1.381 +  }
   1.382 +
   1.383 +  // If we seem to be SGML or XML and we got down here, just pretend we're HTML
   1.384 +  if (*str == '!' || *str == '?') {
   1.385 +    mContentType = TEXT_HTML;
   1.386 +    return true;
   1.387 +  }
   1.388 +  
   1.389 +  uint32_t bufSize = end - str;
   1.390 +  // We use sizeof(_tagstr) below because that's the length of _tagstr
   1.391 +  // with the one char " " or ">" appended.
   1.392 +#define MATCHES_TAG(_tagstr)                                              \
   1.393 +  (bufSize >= sizeof(_tagstr) &&                                          \
   1.394 +   (PL_strncasecmp(str, _tagstr " ", sizeof(_tagstr)) == 0 ||             \
   1.395 +    PL_strncasecmp(str, _tagstr ">", sizeof(_tagstr)) == 0))
   1.396 +    
   1.397 +  if (MATCHES_TAG("html")     ||
   1.398 +      MATCHES_TAG("frameset") ||
   1.399 +      MATCHES_TAG("body")     ||
   1.400 +      MATCHES_TAG("head")     ||
   1.401 +      MATCHES_TAG("script")   ||
   1.402 +      MATCHES_TAG("iframe")   ||
   1.403 +      MATCHES_TAG("a")        ||
   1.404 +      MATCHES_TAG("img")      ||
   1.405 +      MATCHES_TAG("table")    ||
   1.406 +      MATCHES_TAG("title")    ||
   1.407 +      MATCHES_TAG("link")     ||
   1.408 +      MATCHES_TAG("base")     ||
   1.409 +      MATCHES_TAG("style")    ||
   1.410 +      MATCHES_TAG("div")      ||
   1.411 +      MATCHES_TAG("p")        ||
   1.412 +      MATCHES_TAG("font")     ||
   1.413 +      MATCHES_TAG("applet")   ||
   1.414 +      MATCHES_TAG("meta")     ||
   1.415 +      MATCHES_TAG("center")   ||
   1.416 +      MATCHES_TAG("form")     ||
   1.417 +      MATCHES_TAG("isindex")  ||
   1.418 +      MATCHES_TAG("h1")       ||
   1.419 +      MATCHES_TAG("h2")       ||
   1.420 +      MATCHES_TAG("h3")       ||
   1.421 +      MATCHES_TAG("h4")       ||
   1.422 +      MATCHES_TAG("h5")       ||
   1.423 +      MATCHES_TAG("h6")       ||
   1.424 +      MATCHES_TAG("b")        ||
   1.425 +      MATCHES_TAG("pre")) {
   1.426 +  
   1.427 +    mContentType = TEXT_HTML;
   1.428 +    return true;
   1.429 +  }
   1.430 +
   1.431 +#undef MATCHES_TAG
   1.432 +  
   1.433 +  return false;
   1.434 +}
   1.435 +
   1.436 +bool nsUnknownDecoder::SniffForXML(nsIRequest* aRequest)
   1.437 +{
   1.438 +  // Just like HTML, this should be able to be shut off.
   1.439 +  if (!AllowSniffing(aRequest)) {
   1.440 +    return false;
   1.441 +  }
   1.442 +
   1.443 +  // First see whether we can glean anything from the uri...
   1.444 +  if (!SniffURI(aRequest)) {
   1.445 +    // Oh well; just generic XML will have to do
   1.446 +    mContentType = TEXT_XML;
   1.447 +  }
   1.448 +  
   1.449 +  return true;
   1.450 +}
   1.451 +
   1.452 +bool nsUnknownDecoder::SniffURI(nsIRequest* aRequest)
   1.453 +{
   1.454 +  nsCOMPtr<nsIMIMEService> mimeService(do_GetService("@mozilla.org/mime;1"));
   1.455 +  if (mimeService) {
   1.456 +    nsCOMPtr<nsIChannel> channel = do_QueryInterface(aRequest);
   1.457 +    if (channel) {
   1.458 +      nsCOMPtr<nsIURI> uri;
   1.459 +      nsresult result = channel->GetURI(getter_AddRefs(uri));
   1.460 +      if (NS_SUCCEEDED(result) && uri) {
   1.461 +        nsAutoCString type;
   1.462 +        result = mimeService->GetTypeFromURI(uri, type);
   1.463 +        if (NS_SUCCEEDED(result)) {
   1.464 +          mContentType = type;
   1.465 +          return true;
   1.466 +        }
   1.467 +      }
   1.468 +    }
   1.469 +  }
   1.470 +
   1.471 +  return false;
   1.472 +}
   1.473 +
   1.474 +// This macro is based on RFC 2046 Section 4.1.2.  Treat any char 0-31
   1.475 +// except the 9-13 range (\t, \n, \v, \f, \r) and char 27 (used by
   1.476 +// encodings like Shift_JIS) as non-text
   1.477 +#define IS_TEXT_CHAR(ch)                                     \
   1.478 +  (((unsigned char)(ch)) > 31 || (9 <= (ch) && (ch) <= 13) || (ch) == 27)
   1.479 +
   1.480 +bool nsUnknownDecoder::LastDitchSniff(nsIRequest* aRequest)
   1.481 +{
   1.482 +  // All we can do now is try to guess whether this is text/plain or
   1.483 +  // application/octet-stream
   1.484 +
   1.485 +  // First, check for a BOM.  If we see one, assume this is text/plain
   1.486 +  // in whatever encoding.  If there is a BOM _and_ text we will
   1.487 +  // always have at least 4 bytes in the buffer (since the 2-byte BOMs
   1.488 +  // are for 2-byte encodings and the UTF-8 BOM is 3 bytes).
   1.489 +  if (mBufferLen >= 4) {
   1.490 +    const unsigned char* buf = (const unsigned char*)mBuffer;
   1.491 +    if ((buf[0] == 0xFE && buf[1] == 0xFF) || // UTF-16, Big Endian
   1.492 +        (buf[0] == 0xFF && buf[1] == 0xFE) || // UTF-16 or UCS-4, Little Endian
   1.493 +        (buf[0] == 0xEF && buf[1] == 0xBB && buf[2] == 0xBF) || // UTF-8
   1.494 +        (buf[0] == 0 && buf[1] == 0 && buf[2] == 0xFE && buf[3] == 0xFF)) { // UCS-4, Big Endian
   1.495 +        
   1.496 +      mContentType = TEXT_PLAIN;
   1.497 +      return true;
   1.498 +    }
   1.499 +  }
   1.500 +  
   1.501 +  // Now see whether the buffer has any non-text chars.  If not, then let's
   1.502 +  // just call it text/plain...
   1.503 +  //
   1.504 +  uint32_t i;
   1.505 +  for (i = 0; i < mBufferLen && IS_TEXT_CHAR(mBuffer[i]); i++) {
   1.506 +    continue;
   1.507 +  }
   1.508 +
   1.509 +  if (i == mBufferLen) {
   1.510 +    mContentType = TEXT_PLAIN;
   1.511 +  }
   1.512 +  else {
   1.513 +    mContentType = APPLICATION_OCTET_STREAM;
   1.514 +  }
   1.515 +
   1.516 +  return true;    
   1.517 +}
   1.518 +
   1.519 +
   1.520 +nsresult nsUnknownDecoder::FireListenerNotifications(nsIRequest* request,
   1.521 +                                                     nsISupports *aCtxt)
   1.522 +{
   1.523 +  nsresult rv = NS_OK;
   1.524 +
   1.525 +  if (!mNextListener) return NS_ERROR_FAILURE;
   1.526 +
   1.527 +  if (!mContentType.IsEmpty()) {
   1.528 +    nsCOMPtr<nsIViewSourceChannel> viewSourceChannel =
   1.529 +      do_QueryInterface(request);
   1.530 +    if (viewSourceChannel) {
   1.531 +      rv = viewSourceChannel->SetOriginalContentType(mContentType);
   1.532 +    } else {
   1.533 +      nsCOMPtr<nsIChannel> channel = do_QueryInterface(request, &rv);
   1.534 +      if (NS_SUCCEEDED(rv)) {
   1.535 +        // Set the new content type on the channel...
   1.536 +        rv = channel->SetContentType(mContentType);
   1.537 +      }
   1.538 +    }
   1.539 +
   1.540 +    NS_ASSERTION(NS_SUCCEEDED(rv), "Unable to set content type on channel!");
   1.541 +
   1.542 +    if (NS_FAILED(rv)) {
   1.543 +      // Cancel the request to make sure it has the correct status if
   1.544 +      // mNextListener looks at it.
   1.545 +      request->Cancel(rv);
   1.546 +      mNextListener->OnStartRequest(request, aCtxt);
   1.547 +      return rv;
   1.548 +    }
   1.549 +  }
   1.550 +
   1.551 +  // Fire the OnStartRequest(...)
   1.552 +  rv = mNextListener->OnStartRequest(request, aCtxt);
   1.553 +
   1.554 +  if (!mBuffer) return NS_ERROR_OUT_OF_MEMORY;
   1.555 +
   1.556 +  // If the request was canceled, then we need to treat that equivalently
   1.557 +  // to an error returned by OnStartRequest.
   1.558 +  if (NS_SUCCEEDED(rv))
   1.559 +    request->GetStatus(&rv);
   1.560 +
   1.561 +  // Fire the first OnDataAvailable for the data that was read from the
   1.562 +  // stream into the sniffer buffer...
   1.563 +  if (NS_SUCCEEDED(rv) && (mBufferLen > 0)) {
   1.564 +    uint32_t len = 0;
   1.565 +    nsCOMPtr<nsIInputStream> in;
   1.566 +    nsCOMPtr<nsIOutputStream> out;
   1.567 +
   1.568 +    // Create a pipe and fill it with the data from the sniffer buffer.
   1.569 +    rv = NS_NewPipe(getter_AddRefs(in), getter_AddRefs(out),
   1.570 +                    MAX_BUFFER_SIZE, MAX_BUFFER_SIZE);
   1.571 +
   1.572 +    if (NS_SUCCEEDED(rv)) {
   1.573 +      rv = out->Write(mBuffer, mBufferLen, &len);
   1.574 +      if (NS_SUCCEEDED(rv)) {
   1.575 +        if (len == mBufferLen) {
   1.576 +          rv = mNextListener->OnDataAvailable(request, aCtxt, in, 0, len);
   1.577 +        } else {
   1.578 +          NS_ERROR("Unable to write all the data into the pipe.");
   1.579 +          rv = NS_ERROR_FAILURE;
   1.580 +        }
   1.581 +      }
   1.582 +    }
   1.583 +  }
   1.584 +
   1.585 +  delete [] mBuffer;
   1.586 +  mBuffer = nullptr;
   1.587 +  mBufferLen = 0;
   1.588 +
   1.589 +  return rv;
   1.590 +}
   1.591 +
   1.592 +void
   1.593 +nsBinaryDetector::DetermineContentType(nsIRequest* aRequest)
   1.594 +{
   1.595 +  nsCOMPtr<nsIHttpChannel> httpChannel = do_QueryInterface(aRequest);
   1.596 +  if (!httpChannel) {
   1.597 +    return;
   1.598 +  }
   1.599 +
   1.600 +  // It's an HTTP channel.  Check for the text/plain mess
   1.601 +  nsAutoCString contentTypeHdr;
   1.602 +  httpChannel->GetResponseHeader(NS_LITERAL_CSTRING("Content-Type"),
   1.603 +                                 contentTypeHdr);
   1.604 +  nsAutoCString contentType;
   1.605 +  httpChannel->GetContentType(contentType);
   1.606 +
   1.607 +  // Make sure to do a case-sensitive exact match comparison here.  Apache
   1.608 +  // 1.x just sends text/plain for "unknown", while Apache 2.x sends
   1.609 +  // text/plain with a ISO-8859-1 charset.  Debian's Apache version, just to
   1.610 +  // be different, sends text/plain with iso-8859-1 charset.  For extra fun,
   1.611 +  // FC7, RHEL4, and Ubuntu Feisty send charset=UTF-8.  Don't do general
   1.612 +  // case-insensitive comparison, since we really want to apply this crap as
   1.613 +  // rarely as we can.
   1.614 +  if (!contentType.EqualsLiteral("text/plain") ||
   1.615 +      (!contentTypeHdr.EqualsLiteral("text/plain") &&
   1.616 +       !contentTypeHdr.EqualsLiteral("text/plain; charset=ISO-8859-1") &&
   1.617 +       !contentTypeHdr.EqualsLiteral("text/plain; charset=iso-8859-1") &&
   1.618 +       !contentTypeHdr.EqualsLiteral("text/plain; charset=UTF-8"))) {
   1.619 +    return;
   1.620 +  }
   1.621 +
   1.622 +  // Check whether we have content-encoding.  If we do, don't try to
   1.623 +  // detect the type.
   1.624 +  // XXXbz we could improve this by doing a local decompress if we
   1.625 +  // wanted, I'm sure.  
   1.626 +  nsAutoCString contentEncoding;
   1.627 +  httpChannel->GetResponseHeader(NS_LITERAL_CSTRING("Content-Encoding"),
   1.628 +                                 contentEncoding);
   1.629 +  if (!contentEncoding.IsEmpty()) {
   1.630 +    return;
   1.631 +  }
   1.632 +  
   1.633 +  LastDitchSniff(aRequest);
   1.634 +  if (mContentType.Equals(APPLICATION_OCTET_STREAM)) {
   1.635 +    // We want to guess at it instead
   1.636 +    mContentType = APPLICATION_GUESS_FROM_EXT;
   1.637 +  } else {
   1.638 +    // Let the text/plain type we already have be, so that other content
   1.639 +    // sniffers can also get a shot at this data.
   1.640 +    mContentType.Truncate();
   1.641 +  }
   1.642 +}

mercurial