2001-09-29 00:14:13 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
2004-04-19 02:01:16 +04:00
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
2000-01-08 09:17:40 +03:00
|
|
|
*
|
2004-04-19 02:01:16 +04:00
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
2000-01-08 09:17:40 +03:00
|
|
|
*
|
2001-09-29 00:14:13 +04:00
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
2000-01-08 09:17:40 +03:00
|
|
|
*
|
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
2004-04-19 02:01:16 +04:00
|
|
|
* The Initial Developer of the Original Code is
|
2001-09-29 00:14:13 +04:00
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1999
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
2000-01-08 09:17:40 +03:00
|
|
|
*
|
2001-09-29 00:14:13 +04:00
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
2004-04-19 02:01:16 +04:00
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
2001-09-29 00:14:13 +04:00
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
2004-04-19 02:01:16 +04:00
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
2001-09-29 00:14:13 +04:00
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
2004-04-19 02:01:16 +04:00
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
2001-09-29 00:14:13 +04:00
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
2000-01-08 09:17:40 +03:00
|
|
|
|
|
|
|
#include "nsUnknownDecoder.h"
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsIStreamConverterService.h"
|
|
|
|
|
|
|
|
#include "nsIPipe.h"
|
2000-08-22 11:03:33 +04:00
|
|
|
#include "nsIInputStream.h"
|
|
|
|
#include "nsIOutputStream.h"
|
2000-02-09 08:07:48 +03:00
|
|
|
#include "nsMimeTypes.h"
|
2000-01-10 07:08:50 +03:00
|
|
|
#include "netCore.h"
|
2000-10-29 04:57:24 +03:00
|
|
|
#include "nsXPIDLString.h"
|
2003-07-15 01:36:24 +04:00
|
|
|
#include "nsIPrefService.h"
|
|
|
|
#include "nsIPrefBranch.h"
|
2004-07-19 22:53:02 +04:00
|
|
|
#include "nsICategoryManager.h"
|
|
|
|
#include "nsISupportsPrimitives.h"
|
|
|
|
#include "nsIContentSniffer.h"
|
2000-01-08 09:17:40 +03:00
|
|
|
|
2003-04-05 02:46:50 +04:00
|
|
|
#include "nsCRT.h"
|
|
|
|
|
2002-11-07 01:29:17 +03:00
|
|
|
#include "nsIMIMEService.h"
|
|
|
|
|
2002-09-04 02:10:08 +04:00
|
|
|
#include "nsIViewSourceChannel.h"
|
2007-08-21 07:26:12 +04:00
|
|
|
#include "nsIHttpChannel.h"
|
|
|
|
#include "nsNetCID.h"
|
2002-09-04 02:10:08 +04:00
|
|
|
|
2001-11-03 10:10:51 +03:00
|
|
|
|
2009-04-06 04:30:03 +04:00
|
|
|
#define MAX_BUFFER_SIZE 512
|
2000-01-08 09:17:40 +03:00
|
|
|
|
|
|
|
nsUnknownDecoder::nsUnknownDecoder()
|
2003-07-15 01:36:24 +04:00
|
|
|
: mBuffer(nsnull)
|
|
|
|
, mBufferLen(0)
|
|
|
|
, mRequireHTMLsuffix(PR_FALSE)
|
2000-01-08 09:17:40 +03:00
|
|
|
{
|
2003-07-15 01:36:24 +04:00
|
|
|
nsCOMPtr<nsIPrefBranch> prefs = do_GetService(NS_PREFSERVICE_CONTRACTID);
|
|
|
|
if (prefs) {
|
|
|
|
PRBool val;
|
|
|
|
if (NS_SUCCEEDED(prefs->GetBoolPref("security.requireHTMLsuffix", &val)))
|
|
|
|
mRequireHTMLsuffix = val;
|
2000-10-29 04:57:24 +03:00
|
|
|
}
|
2000-01-08 09:17:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsUnknownDecoder::~nsUnknownDecoder()
|
|
|
|
{
|
|
|
|
if (mBuffer) {
|
|
|
|
delete [] mBuffer;
|
|
|
|
mBuffer = nsnull;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----
|
|
|
|
//
|
|
|
|
// nsISupports implementation...
|
|
|
|
//
|
|
|
|
// ----
|
|
|
|
|
2003-09-08 03:12:15 +04:00
|
|
|
NS_IMPL_ADDREF(nsUnknownDecoder)
|
|
|
|
NS_IMPL_RELEASE(nsUnknownDecoder)
|
2000-01-08 09:17:40 +03:00
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN(nsUnknownDecoder)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIStreamConverter)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIStreamListener)
|
2001-04-10 10:01:08 +04:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIRequestObserver)
|
2006-02-17 19:54:27 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIContentSniffer)
|
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIStreamListener)
|
2000-01-08 09:17:40 +03:00
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
|
|
|
|
// ----
|
|
|
|
//
|
|
|
|
// nsIStreamConverter methods...
|
|
|
|
//
|
|
|
|
// ----
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsUnknownDecoder::Convert(nsIInputStream *aFromStream,
|
2004-06-29 20:45:07 +04:00
|
|
|
const char *aFromType,
|
|
|
|
const char *aToType,
|
2000-01-08 09:17:40 +03:00
|
|
|
nsISupports *aCtxt,
|
|
|
|
nsIInputStream **aResultStream)
|
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2004-06-29 20:45:07 +04:00
|
|
|
nsUnknownDecoder::AsyncConvertData(const char *aFromType,
|
|
|
|
const char *aToType,
|
2000-01-08 09:17:40 +03:00
|
|
|
nsIStreamListener *aListener,
|
|
|
|
nsISupports *aCtxt)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aListener && aFromType && aToType,
|
|
|
|
"null pointer passed into multi mixed converter");
|
|
|
|
// hook up our final listener. this guy gets the various On*() calls we want to throw
|
|
|
|
// at him.
|
|
|
|
//
|
|
|
|
mNextListener = aListener;
|
|
|
|
return (aListener) ? NS_OK : NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----
|
|
|
|
//
|
|
|
|
// nsIStreamListener methods...
|
|
|
|
//
|
|
|
|
// ----
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2001-02-21 23:38:08 +03:00
|
|
|
nsUnknownDecoder::OnDataAvailable(nsIRequest* request,
|
2000-01-08 09:17:40 +03:00
|
|
|
nsISupports *aCtxt,
|
|
|
|
nsIInputStream *aStream,
|
|
|
|
PRUint32 aSourceOffset,
|
|
|
|
PRUint32 aCount)
|
|
|
|
{
|
2000-11-16 01:21:46 +03:00
|
|
|
nsresult rv = NS_OK;
|
2000-01-08 09:17:40 +03:00
|
|
|
|
|
|
|
if (!mNextListener) return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
if (mContentType.IsEmpty()) {
|
|
|
|
PRUint32 count, len;
|
|
|
|
|
|
|
|
// If the buffer has not been allocated by now, just fail...
|
|
|
|
if (!mBuffer) return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
//
|
|
|
|
// Determine how much of the stream should be read to fill up the
|
|
|
|
// sniffer buffer...
|
|
|
|
//
|
|
|
|
if (mBufferLen + aCount >= MAX_BUFFER_SIZE) {
|
|
|
|
count = MAX_BUFFER_SIZE-mBufferLen;
|
|
|
|
} else {
|
|
|
|
count = aCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the data into the buffer...
|
|
|
|
rv = aStream->Read((mBuffer+mBufferLen), count, &len);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
mBufferLen += len;
|
|
|
|
aCount -= len;
|
|
|
|
|
|
|
|
if (aCount) {
|
|
|
|
//
|
|
|
|
// Adjust the source offset... The call to FireListenerNotifications(...)
|
|
|
|
// will make the first OnDataAvailable(...) call with an offset of 0.
|
|
|
|
// So, this offset needs to be adjusted to reflect that...
|
|
|
|
//
|
2000-01-10 07:34:52 +03:00
|
|
|
aSourceOffset += mBufferLen;
|
2000-01-08 09:17:40 +03:00
|
|
|
|
2001-02-21 23:38:08 +03:00
|
|
|
DetermineContentType(request);
|
2000-01-08 09:17:40 +03:00
|
|
|
|
2001-02-21 23:38:08 +03:00
|
|
|
rv = FireListenerNotifications(request, aCtxt);
|
2000-01-08 09:17:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-04-21 22:21:48 +04:00
|
|
|
// Must not fire ODA again if it failed once
|
|
|
|
if (aCount && NS_SUCCEEDED(rv)) {
|
2000-01-08 09:17:40 +03:00
|
|
|
NS_ASSERTION(!mContentType.IsEmpty(),
|
|
|
|
"Content type should be known by now.");
|
|
|
|
|
2001-02-21 23:38:08 +03:00
|
|
|
rv = mNextListener->OnDataAvailable(request, aCtxt, aStream,
|
2000-01-08 09:17:40 +03:00
|
|
|
aSourceOffset, aCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----
|
|
|
|
//
|
2001-04-10 10:01:08 +04:00
|
|
|
// nsIRequestObserver methods...
|
2000-01-08 09:17:40 +03:00
|
|
|
//
|
|
|
|
// ----
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2001-02-21 23:38:08 +03:00
|
|
|
nsUnknownDecoder::OnStartRequest(nsIRequest* request, nsISupports *aCtxt)
|
2000-01-08 09:17:40 +03:00
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
|
|
|
if (!mNextListener) return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
// Allocate the sniffer buffer...
|
|
|
|
if (NS_SUCCEEDED(rv) && !mBuffer) {
|
|
|
|
mBuffer = new char[MAX_BUFFER_SIZE];
|
|
|
|
|
|
|
|
if (!mBuffer) {
|
|
|
|
rv = NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do not pass the OnStartRequest on to the next listener (yet)...
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2001-02-21 23:38:08 +03:00
|
|
|
nsUnknownDecoder::OnStopRequest(nsIRequest* request, nsISupports *aCtxt,
|
2001-04-10 10:01:08 +04:00
|
|
|
nsresult aStatus)
|
2000-01-08 09:17:40 +03:00
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
|
|
|
if (!mNextListener) return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
//
|
|
|
|
// The total amount of data is less than the size of the sniffer buffer.
|
|
|
|
// Analyze the buffer now...
|
|
|
|
//
|
|
|
|
if (mContentType.IsEmpty()) {
|
2001-02-21 23:38:08 +03:00
|
|
|
DetermineContentType(request);
|
2000-01-08 09:17:40 +03:00
|
|
|
|
2001-02-21 23:38:08 +03:00
|
|
|
rv = FireListenerNotifications(request, aCtxt);
|
2000-01-08 09:17:40 +03:00
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aStatus = rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-04-10 10:01:08 +04:00
|
|
|
rv = mNextListener->OnStopRequest(request, aCtxt, aStatus);
|
2000-01-08 09:17:40 +03:00
|
|
|
mNextListener = 0;
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2006-02-17 19:54:27 +03:00
|
|
|
// ----
|
|
|
|
//
|
|
|
|
// nsIContentSniffer methods...
|
|
|
|
//
|
|
|
|
// ----
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsUnknownDecoder::GetMIMETypeFromContent(nsIRequest* aRequest,
|
|
|
|
const PRUint8* aData,
|
|
|
|
PRUint32 aLength,
|
|
|
|
nsACString& type)
|
|
|
|
{
|
2007-07-08 11:08:04 +04:00
|
|
|
mBuffer = const_cast<char*>(reinterpret_cast<const char*>(aData));
|
2006-02-17 19:54:27 +03:00
|
|
|
mBufferLen = aLength;
|
|
|
|
DetermineContentType(aRequest);
|
|
|
|
mBuffer = nsnull;
|
|
|
|
mBufferLen = 0;
|
|
|
|
type.Assign(mContentType);
|
|
|
|
mContentType.Truncate();
|
2007-09-17 19:53:57 +04:00
|
|
|
return type.IsEmpty() ? NS_ERROR_NOT_AVAILABLE : NS_OK;
|
2006-02-17 19:54:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-01-11 22:34:13 +03:00
|
|
|
// Actual sniffing code
|
2000-01-08 09:17:40 +03:00
|
|
|
|
2003-01-11 22:34:13 +03:00
|
|
|
PRBool nsUnknownDecoder::AllowSniffing(nsIRequest* aRequest)
|
2000-01-08 09:17:40 +03:00
|
|
|
{
|
2003-01-11 22:34:13 +03:00
|
|
|
if (!mRequireHTMLsuffix) {
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
2001-11-19 23:25:53 +03:00
|
|
|
|
2003-01-11 22:34:13 +03:00
|
|
|
nsCOMPtr<nsIChannel> channel = do_QueryInterface(aRequest);
|
|
|
|
if (!channel) {
|
|
|
|
NS_ERROR("QI failed");
|
|
|
|
return PR_FALSE;
|
2001-11-19 23:25:53 +03:00
|
|
|
}
|
2003-01-11 22:34:13 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
if (NS_FAILED(channel->GetURI(getter_AddRefs(uri))) || !uri) {
|
|
|
|
return PR_FALSE;
|
2001-11-19 23:25:53 +03:00
|
|
|
}
|
2003-01-11 22:34:13 +03:00
|
|
|
|
|
|
|
PRBool isLocalFile = PR_FALSE;
|
|
|
|
if (NS_FAILED(uri->SchemeIs("file", &isLocalFile)) || isLocalFile) {
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is the array of sniffer entries that depend on "magic numbers"
|
|
|
|
* in the file. Each entry has either a type associated with it (set
|
|
|
|
* these with the SNIFFER_ENTRY macro) or a function to be executed
|
|
|
|
* (set these with the SNIFFER_ENTRY_WITH_FUNC macro). The function
|
|
|
|
* should take a single nsIRequest* and returns PRBool -- PR_TRUE if
|
|
|
|
* it sets mContentType, PR_FALSE otherwise
|
|
|
|
*/
|
|
|
|
nsUnknownDecoder::nsSnifferEntry nsUnknownDecoder::sSnifferEntries[] = {
|
|
|
|
SNIFFER_ENTRY("%PDF-", APPLICATION_PDF),
|
|
|
|
|
|
|
|
SNIFFER_ENTRY("%!PS-Adobe-", APPLICATION_POSTSCRIPT),
|
|
|
|
|
2005-02-05 03:49:25 +03:00
|
|
|
// Files that start with mailbox delimiters let's provisionally call
|
2003-01-11 22:34:13 +03:00
|
|
|
// text/plain
|
|
|
|
SNIFFER_ENTRY("From", TEXT_PLAIN),
|
|
|
|
SNIFFER_ENTRY(">From", TEXT_PLAIN),
|
|
|
|
|
2001-11-28 00:20:25 +03:00
|
|
|
// If the buffer begins with "#!" or "%!" then it is a script of
|
|
|
|
// some sort... "Scripts" can include arbitrary data to be passed
|
2003-01-11 22:34:13 +03:00
|
|
|
// to an interpreter, so we need to decide whether we can call this
|
|
|
|
// text or whether it's data.
|
2003-01-12 00:25:57 +03:00
|
|
|
SNIFFER_ENTRY_WITH_FUNC("#!", &nsUnknownDecoder::LastDitchSniff),
|
2000-01-08 09:17:40 +03:00
|
|
|
|
2004-01-08 06:56:02 +03:00
|
|
|
// XXXbz should (and can) we also include the various ways that <?xml can
|
|
|
|
// appear as UTF-16 and such? See http://www.w3.org/TR/REC-xml#sec-guessing
|
2003-01-12 00:25:57 +03:00
|
|
|
SNIFFER_ENTRY_WITH_FUNC("<?xml", &nsUnknownDecoder::SniffForXML)
|
2003-01-11 22:34:13 +03:00
|
|
|
};
|
2000-10-29 04:57:24 +03:00
|
|
|
|
2003-01-11 22:34:13 +03:00
|
|
|
PRUint32 nsUnknownDecoder::sSnifferEntryNum =
|
|
|
|
sizeof(nsUnknownDecoder::sSnifferEntries) /
|
|
|
|
sizeof(nsUnknownDecoder::nsSnifferEntry);
|
2001-02-21 23:38:08 +03:00
|
|
|
|
2003-01-11 22:34:13 +03:00
|
|
|
void nsUnknownDecoder::DetermineContentType(nsIRequest* aRequest)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mContentType.IsEmpty(), "Content type is already known.");
|
|
|
|
if (!mContentType.IsEmpty()) return;
|
|
|
|
|
|
|
|
// First, run through all the types we can detect reliably based on
|
|
|
|
// magic numbers
|
|
|
|
PRUint32 i;
|
|
|
|
for (i = 0; i < sSnifferEntryNum; ++i) {
|
|
|
|
if (mBufferLen >= sSnifferEntries[i].mByteLen && // enough data
|
|
|
|
memcmp(mBuffer, sSnifferEntries[i].mBytes, sSnifferEntries[i].mByteLen) == 0) { // and type matches
|
|
|
|
NS_ASSERTION(sSnifferEntries[i].mMimeType ||
|
|
|
|
sSnifferEntries[i].mContentTypeSniffer,
|
|
|
|
"Must have either a type string or a function to set the type");
|
2006-02-22 15:57:23 +03:00
|
|
|
NS_ASSERTION(sSnifferEntries[i].mMimeType == nsnull ||
|
|
|
|
sSnifferEntries[i].mContentTypeSniffer == nsnull,
|
|
|
|
"Both a type string and a type sniffing function set;"
|
2003-01-11 22:34:13 +03:00
|
|
|
" using type string");
|
|
|
|
if (sSnifferEntries[i].mMimeType) {
|
|
|
|
mContentType = sSnifferEntries[i].mMimeType;
|
2007-08-21 07:26:12 +04:00
|
|
|
NS_ASSERTION(!mContentType.IsEmpty(),
|
|
|
|
"Content type should be known by now.");
|
2003-01-11 22:34:13 +03:00
|
|
|
return;
|
|
|
|
}
|
2007-08-21 07:26:12 +04:00
|
|
|
if ((this->*(sSnifferEntries[i].mContentTypeSniffer))(aRequest)) {
|
|
|
|
NS_ASSERTION(!mContentType.IsEmpty(),
|
|
|
|
"Content type should be known by now.");
|
2003-01-11 22:34:13 +03:00
|
|
|
return;
|
|
|
|
}
|
2000-10-29 04:57:24 +03:00
|
|
|
}
|
2003-01-11 22:34:13 +03:00
|
|
|
}
|
2000-10-29 04:57:24 +03:00
|
|
|
|
2004-07-19 22:53:02 +04:00
|
|
|
if (TryContentSniffers(aRequest)) {
|
2007-08-21 07:26:12 +04:00
|
|
|
NS_ASSERTION(!mContentType.IsEmpty(),
|
|
|
|
"Content type should be known by now.");
|
2003-01-11 22:34:13 +03:00
|
|
|
return;
|
|
|
|
}
|
2000-10-29 04:57:24 +03:00
|
|
|
|
2003-04-05 02:46:50 +04:00
|
|
|
if (SniffForHTML(aRequest)) {
|
2007-08-21 07:26:12 +04:00
|
|
|
NS_ASSERTION(!mContentType.IsEmpty(),
|
|
|
|
"Content type should be known by now.");
|
2003-04-05 02:46:50 +04:00
|
|
|
return;
|
2000-06-16 10:15:13 +04:00
|
|
|
}
|
2003-04-05 02:46:50 +04:00
|
|
|
|
2003-01-11 22:34:13 +03:00
|
|
|
// We don't know what this is yet. Before we just give up, try
|
|
|
|
// the URI from the request.
|
|
|
|
if (SniffURI(aRequest)) {
|
2007-08-21 07:26:12 +04:00
|
|
|
NS_ASSERTION(!mContentType.IsEmpty(),
|
|
|
|
"Content type should be known by now.");
|
2003-01-11 22:34:13 +03:00
|
|
|
return;
|
2001-06-14 01:24:11 +04:00
|
|
|
}
|
2003-01-11 22:34:13 +03:00
|
|
|
|
|
|
|
LastDitchSniff(aRequest);
|
2007-08-21 07:26:12 +04:00
|
|
|
NS_ASSERTION(!mContentType.IsEmpty(),
|
|
|
|
"Content type should be known by now.");
|
2003-01-11 22:34:13 +03:00
|
|
|
}
|
2002-11-07 01:29:17 +03:00
|
|
|
|
2004-07-19 22:53:02 +04:00
|
|
|
PRBool nsUnknownDecoder::TryContentSniffers(nsIRequest* aRequest)
|
2003-01-11 22:34:13 +03:00
|
|
|
{
|
2004-07-19 22:53:02 +04:00
|
|
|
// Enumerate content sniffers
|
|
|
|
nsCOMPtr<nsICategoryManager> catMan(do_GetService("@mozilla.org/categorymanager;1"));
|
|
|
|
if (!catMan) {
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
2003-04-03 23:36:27 +04:00
|
|
|
|
2004-07-19 22:53:02 +04:00
|
|
|
nsCOMPtr<nsISimpleEnumerator> sniffers;
|
|
|
|
catMan->EnumerateCategory("content-sniffing-services", getter_AddRefs(sniffers));
|
|
|
|
if (!sniffers) {
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
2003-01-11 22:34:13 +03:00
|
|
|
|
2004-07-19 22:53:02 +04:00
|
|
|
PRBool hasMore;
|
|
|
|
while (NS_SUCCEEDED(sniffers->HasMoreElements(&hasMore)) && hasMore) {
|
|
|
|
nsCOMPtr<nsISupports> elem;
|
|
|
|
sniffers->GetNext(getter_AddRefs(elem));
|
|
|
|
NS_ASSERTION(elem, "No element even though hasMore returned true!?");
|
|
|
|
|
|
|
|
nsCOMPtr<nsISupportsCString> sniffer_id(do_QueryInterface(elem));
|
|
|
|
NS_ASSERTION(sniffer_id, "element is no nsISupportsCString!?");
|
|
|
|
nsCAutoString contractid;
|
|
|
|
nsresult rv = sniffer_id->GetData(contractid);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIContentSniffer> sniffer(do_GetService(contractid.get()));
|
|
|
|
if (!sniffer) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2006-02-17 19:54:27 +03:00
|
|
|
rv = sniffer->GetMIMETypeFromContent(aRequest, (const PRUint8*)mBuffer,
|
|
|
|
mBufferLen, mContentType);
|
2004-07-19 22:53:02 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return PR_FALSE;
|
2003-01-11 22:34:13 +03:00
|
|
|
}
|
|
|
|
|
2003-04-05 02:46:50 +04:00
|
|
|
PRBool nsUnknownDecoder::SniffForHTML(nsIRequest* aRequest)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* To prevent a possible attack, we will not consider this to be
|
|
|
|
* html content if it comes from the local file system and our prefs
|
|
|
|
* are set right
|
|
|
|
*/
|
|
|
|
if (!AllowSniffing(aRequest)) {
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
2003-10-05 00:27:32 +04:00
|
|
|
// Now look for HTML.
|
|
|
|
const char* str = mBuffer;
|
|
|
|
const char* end = mBuffer + mBufferLen;
|
2003-04-05 02:46:50 +04:00
|
|
|
|
|
|
|
// skip leading whitespace
|
2003-10-05 00:27:32 +04:00
|
|
|
while (str != end && nsCRT::IsAsciiSpace(*str)) {
|
|
|
|
++str;
|
2003-04-05 02:46:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// did we find something like a start tag?
|
2003-10-05 00:27:32 +04:00
|
|
|
if (str == end || *str != '<' || ++str == end) {
|
2003-04-05 02:46:50 +04:00
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we seem to be SGML or XML and we got down here, just pretend we're HTML
|
2003-10-05 00:27:32 +04:00
|
|
|
if (*str == '!' || *str == '?') {
|
2003-04-05 02:46:50 +04:00
|
|
|
mContentType = TEXT_HTML;
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
2003-10-05 00:27:32 +04:00
|
|
|
|
|
|
|
PRUint32 bufSize = end - str;
|
2003-07-31 03:16:29 +04:00
|
|
|
// We use sizeof(_tagstr) below because that's the length of _tagstr
|
|
|
|
// with the one char " " or ">" appended.
|
|
|
|
#define MATCHES_TAG(_tagstr) \
|
2003-10-05 00:27:32 +04:00
|
|
|
(bufSize >= sizeof(_tagstr) && \
|
|
|
|
(PL_strncasecmp(str, _tagstr " ", sizeof(_tagstr)) == 0 || \
|
|
|
|
PL_strncasecmp(str, _tagstr ">", sizeof(_tagstr)) == 0))
|
2003-07-31 03:16:29 +04:00
|
|
|
|
2003-04-05 02:46:50 +04:00
|
|
|
if (MATCHES_TAG("html") ||
|
|
|
|
MATCHES_TAG("frameset") ||
|
|
|
|
MATCHES_TAG("body") ||
|
2003-07-22 21:06:46 +04:00
|
|
|
MATCHES_TAG("head") ||
|
2003-04-05 02:46:50 +04:00
|
|
|
MATCHES_TAG("script") ||
|
2003-11-14 02:22:49 +03:00
|
|
|
MATCHES_TAG("iframe") ||
|
2003-07-30 21:58:56 +04:00
|
|
|
MATCHES_TAG("a") ||
|
2003-04-05 02:46:50 +04:00
|
|
|
MATCHES_TAG("img") ||
|
|
|
|
MATCHES_TAG("table") ||
|
|
|
|
MATCHES_TAG("title") ||
|
2003-07-30 21:58:56 +04:00
|
|
|
MATCHES_TAG("link") ||
|
|
|
|
MATCHES_TAG("base") ||
|
|
|
|
MATCHES_TAG("style") ||
|
2003-04-05 02:46:50 +04:00
|
|
|
MATCHES_TAG("div") ||
|
2003-07-30 21:58:56 +04:00
|
|
|
MATCHES_TAG("p") ||
|
|
|
|
MATCHES_TAG("font") ||
|
2003-04-05 02:46:50 +04:00
|
|
|
MATCHES_TAG("applet") ||
|
2003-07-30 21:58:56 +04:00
|
|
|
MATCHES_TAG("meta") ||
|
|
|
|
MATCHES_TAG("center") ||
|
|
|
|
MATCHES_TAG("form") ||
|
|
|
|
MATCHES_TAG("isindex") ||
|
|
|
|
MATCHES_TAG("h1") ||
|
|
|
|
MATCHES_TAG("h2") ||
|
|
|
|
MATCHES_TAG("h3") ||
|
|
|
|
MATCHES_TAG("h4") ||
|
|
|
|
MATCHES_TAG("h5") ||
|
|
|
|
MATCHES_TAG("h6") ||
|
|
|
|
MATCHES_TAG("b") ||
|
|
|
|
MATCHES_TAG("pre")) {
|
2003-04-05 02:46:50 +04:00
|
|
|
|
|
|
|
mContentType = TEXT_HTML;
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef MATCHES_TAG
|
|
|
|
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
2003-01-11 22:34:13 +03:00
|
|
|
PRBool nsUnknownDecoder::SniffForXML(nsIRequest* aRequest)
|
|
|
|
{
|
|
|
|
// Just like HTML, this should be able to be shut off.
|
|
|
|
if (!AllowSniffing(aRequest)) {
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// First see whether we can glean anything from the uri...
|
|
|
|
if (!SniffURI(aRequest)) {
|
|
|
|
// Oh well; just generic XML will have to do
|
|
|
|
mContentType = TEXT_XML;
|
|
|
|
}
|
|
|
|
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool nsUnknownDecoder::SniffURI(nsIRequest* aRequest)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIMIMEService> mimeService(do_GetService("@mozilla.org/mime;1"));
|
|
|
|
if (mimeService) {
|
|
|
|
nsCOMPtr<nsIChannel> channel = do_QueryInterface(aRequest);
|
|
|
|
if (channel) {
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
nsresult result = channel->GetURI(getter_AddRefs(uri));
|
|
|
|
if (NS_SUCCEEDED(result) && uri) {
|
2004-04-16 00:56:37 +04:00
|
|
|
nsCAutoString type;
|
|
|
|
result = mimeService->GetTypeFromURI(uri, type);
|
2003-01-11 22:34:13 +03:00
|
|
|
if (NS_SUCCEEDED(result)) {
|
|
|
|
mContentType = type;
|
|
|
|
return PR_TRUE;
|
2002-11-07 01:29:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2003-01-11 22:34:13 +03:00
|
|
|
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
2003-11-18 18:10:56 +03:00
|
|
|
// This macro is based on RFC 2046 Section 4.1.2. Treat any char 0-31
|
2004-01-08 06:56:02 +03:00
|
|
|
// except the 9-13 range (\t, \n, \v, \f, \r) and char 27 (used by
|
|
|
|
// encodings like Shift_JIS) as non-text
|
2003-11-18 18:10:56 +03:00
|
|
|
#define IS_TEXT_CHAR(ch) \
|
2004-01-08 06:56:02 +03:00
|
|
|
(((unsigned char)(ch)) > 31 || (9 <= (ch) && (ch) <= 13) || (ch) == 27)
|
2003-11-18 18:10:56 +03:00
|
|
|
|
2003-01-11 22:34:13 +03:00
|
|
|
PRBool nsUnknownDecoder::LastDitchSniff(nsIRequest* aRequest)
|
|
|
|
{
|
|
|
|
// All we can do now is try to guess whether this is text/plain or
|
|
|
|
// application/octet-stream
|
2004-01-08 06:56:02 +03:00
|
|
|
|
|
|
|
// First, check for a BOM. If we see one, assume this is text/plain
|
|
|
|
// in whatever encoding. If there is a BOM _and_ text we will
|
|
|
|
// always have at least 4 bytes in the buffer (since the 2-byte BOMs
|
|
|
|
// are for 2-byte encodings and the UTF-8 BOM is 3 bytes).
|
|
|
|
if (mBufferLen >= 4) {
|
|
|
|
const unsigned char* buf = (const unsigned char*)mBuffer;
|
2008-12-06 22:08:26 +03:00
|
|
|
if ((buf[0] == 0xFE && buf[1] == 0xFF) || // UTF-16, Big Endian
|
|
|
|
(buf[0] == 0xFF && buf[1] == 0xFE) || // UTF-16 or UCS-4, Little Endian
|
2004-01-08 06:56:02 +03:00
|
|
|
(buf[0] == 0xEF && buf[1] == 0xBB && buf[2] == 0xBF) || // UTF-8
|
2008-12-06 22:08:26 +03:00
|
|
|
(buf[0] == 0 && buf[1] == 0 && buf[2] == 0xFE && buf[3] == 0xFF)) { // UCS-4, Big Endian
|
2004-01-08 06:56:02 +03:00
|
|
|
|
|
|
|
mContentType = TEXT_PLAIN;
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now see whether the buffer has any non-text chars. If not, then let's
|
|
|
|
// just call it text/plain...
|
2000-06-16 10:15:13 +04:00
|
|
|
//
|
2003-01-11 22:34:13 +03:00
|
|
|
PRUint32 i;
|
2003-11-18 18:10:56 +03:00
|
|
|
for (i=0; i<mBufferLen && IS_TEXT_CHAR(mBuffer[i]); i++);
|
2000-01-08 09:17:40 +03:00
|
|
|
|
2003-01-11 22:34:13 +03:00
|
|
|
if (i == mBufferLen) {
|
|
|
|
mContentType = TEXT_PLAIN;
|
|
|
|
}
|
|
|
|
else {
|
2004-03-31 03:52:00 +04:00
|
|
|
mContentType = APPLICATION_OCTET_STREAM;
|
2000-01-08 09:17:40 +03:00
|
|
|
}
|
|
|
|
|
2003-01-11 22:34:13 +03:00
|
|
|
return PR_TRUE;
|
2001-06-14 01:24:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-02-21 23:38:08 +03:00
|
|
|
nsresult nsUnknownDecoder::FireListenerNotifications(nsIRequest* request,
|
2000-01-08 09:17:40 +03:00
|
|
|
nsISupports *aCtxt)
|
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
|
|
|
if (!mNextListener) return NS_ERROR_FAILURE;
|
|
|
|
|
2007-08-21 07:26:12 +04:00
|
|
|
if (!mContentType.IsEmpty()) {
|
|
|
|
nsCOMPtr<nsIViewSourceChannel> viewSourceChannel =
|
|
|
|
do_QueryInterface(request);
|
|
|
|
if (viewSourceChannel) {
|
|
|
|
rv = viewSourceChannel->SetOriginalContentType(mContentType);
|
|
|
|
} else {
|
|
|
|
nsCOMPtr<nsIChannel> channel = do_QueryInterface(request, &rv);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
// Set the new content type on the channel...
|
|
|
|
rv = channel->SetContentType(mContentType);
|
|
|
|
}
|
2005-04-21 22:21:48 +04:00
|
|
|
}
|
2001-05-19 01:02:01 +04:00
|
|
|
|
2007-08-21 07:26:12 +04:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "Unable to set content type on channel!");
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// Cancel the request to make sure it has the correct status if
|
|
|
|
// mNextListener looks at it.
|
|
|
|
request->Cancel(rv);
|
|
|
|
mNextListener->OnStartRequest(request, aCtxt);
|
|
|
|
return rv;
|
|
|
|
}
|
2005-04-21 22:21:48 +04:00
|
|
|
}
|
2000-01-08 09:17:40 +03:00
|
|
|
|
|
|
|
// Fire the OnStartRequest(...)
|
2001-02-21 23:38:08 +03:00
|
|
|
rv = mNextListener->OnStartRequest(request, aCtxt);
|
2000-01-08 09:17:40 +03:00
|
|
|
|
2005-04-21 22:21:48 +04:00
|
|
|
if (!mBuffer) return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
2006-08-01 21:17:32 +04:00
|
|
|
// If the request was canceled, then we need to treat that equivalently
|
|
|
|
// to an error returned by OnStartRequest.
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
request->GetStatus(&rv);
|
|
|
|
|
2000-01-08 09:17:40 +03:00
|
|
|
// Fire the first OnDataAvailable for the data that was read from the
|
|
|
|
// stream into the sniffer buffer...
|
2001-05-19 01:02:01 +04:00
|
|
|
if (NS_SUCCEEDED(rv) && (mBufferLen > 0)) {
|
2000-01-08 09:17:40 +03:00
|
|
|
PRUint32 len = 0;
|
2000-08-22 11:03:33 +04:00
|
|
|
nsCOMPtr<nsIInputStream> in;
|
|
|
|
nsCOMPtr<nsIOutputStream> out;
|
2000-01-08 09:17:40 +03:00
|
|
|
|
|
|
|
// Create a pipe and fill it with the data from the sniffer buffer.
|
2000-08-22 11:03:33 +04:00
|
|
|
rv = NS_NewPipe(getter_AddRefs(in), getter_AddRefs(out),
|
2000-01-08 09:17:40 +03:00
|
|
|
MAX_BUFFER_SIZE, MAX_BUFFER_SIZE);
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = out->Write(mBuffer, mBufferLen, &len);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2001-05-19 01:02:01 +04:00
|
|
|
if (len == mBufferLen) {
|
2001-02-21 23:38:08 +03:00
|
|
|
rv = mNextListener->OnDataAvailable(request, aCtxt, in, 0, len);
|
2000-01-08 09:17:40 +03:00
|
|
|
} else {
|
2005-04-21 22:21:48 +04:00
|
|
|
NS_ERROR("Unable to write all the data into the pipe.");
|
2000-01-08 09:17:40 +03:00
|
|
|
rv = NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
delete [] mBuffer;
|
|
|
|
mBuffer = nsnull;
|
|
|
|
mBufferLen = 0;
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
2004-01-08 06:56:02 +03:00
|
|
|
|
|
|
|
void
|
|
|
|
nsBinaryDetector::DetermineContentType(nsIRequest* aRequest)
|
|
|
|
{
|
2007-08-21 07:26:12 +04:00
|
|
|
nsCOMPtr<nsIHttpChannel> httpChannel = do_QueryInterface(aRequest);
|
|
|
|
if (!httpChannel) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// It's an HTTP channel. Check for the text/plain mess
|
|
|
|
nsCAutoString contentTypeHdr;
|
|
|
|
httpChannel->GetResponseHeader(NS_LITERAL_CSTRING("Content-Type"),
|
|
|
|
contentTypeHdr);
|
|
|
|
nsCAutoString contentType;
|
|
|
|
httpChannel->GetContentType(contentType);
|
|
|
|
|
|
|
|
// Make sure to do a case-sensitive exact match comparison here. Apache
|
|
|
|
// 1.x just sends text/plain for "unknown", while Apache 2.x sends
|
|
|
|
// text/plain with a ISO-8859-1 charset. Debian's Apache version, just to
|
2007-12-03 21:37:54 +03:00
|
|
|
// be different, sends text/plain with iso-8859-1 charset. For extra fun,
|
|
|
|
// FC7, RHEL4, and Ubuntu Feisty send charset=UTF-8. Don't do general
|
|
|
|
// case-insensitive comparison, since we really want to apply this crap as
|
|
|
|
// rarely as we can.
|
2007-08-21 07:26:12 +04:00
|
|
|
if (!contentType.EqualsLiteral("text/plain") ||
|
|
|
|
(!contentTypeHdr.EqualsLiteral("text/plain") &&
|
|
|
|
!contentTypeHdr.EqualsLiteral("text/plain; charset=ISO-8859-1") &&
|
2007-12-03 21:37:54 +03:00
|
|
|
!contentTypeHdr.EqualsLiteral("text/plain; charset=iso-8859-1") &&
|
|
|
|
!contentTypeHdr.EqualsLiteral("text/plain; charset=UTF-8"))) {
|
2007-08-21 07:26:12 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check whether we have content-encoding. If we do, don't try to
|
|
|
|
// detect the type.
|
|
|
|
// XXXbz we could improve this by doing a local decompress if we
|
|
|
|
// wanted, I'm sure.
|
|
|
|
nsCAutoString contentEncoding;
|
|
|
|
httpChannel->GetResponseHeader(NS_LITERAL_CSTRING("Content-Encoding"),
|
|
|
|
contentEncoding);
|
|
|
|
if (!contentEncoding.IsEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-01-08 06:56:02 +03:00
|
|
|
LastDitchSniff(aRequest);
|
2004-03-31 03:52:00 +04:00
|
|
|
if (mContentType.Equals(APPLICATION_OCTET_STREAM)) {
|
|
|
|
// We want to guess at it instead
|
|
|
|
mContentType = APPLICATION_GUESS_FROM_EXT;
|
2007-09-17 19:53:57 +04:00
|
|
|
} else {
|
|
|
|
// Let the text/plain type we already have be, so that other content
|
|
|
|
// sniffers can also get a shot at this data.
|
|
|
|
mContentType.Truncate();
|
2004-03-31 03:52:00 +04:00
|
|
|
}
|
2004-01-08 06:56:02 +03:00
|
|
|
}
|
2007-08-21 07:26:12 +04:00
|
|
|
|
|
|
|
NS_METHOD
|
|
|
|
nsBinaryDetector::Register(nsIComponentManager* compMgr, nsIFile* path,
|
|
|
|
const char* registryLocation,
|
|
|
|
const char* componentType,
|
|
|
|
const nsModuleComponentInfo *info)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsICategoryManager> catman =
|
|
|
|
do_GetService(NS_CATEGORYMANAGER_CONTRACTID, &rv);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
return catman->AddCategoryEntry(NS_CONTENT_SNIFFER_CATEGORY,
|
|
|
|
"Binary Detector",
|
|
|
|
NS_BINARYDETECTOR_CONTRACTID,
|
|
|
|
PR_TRUE, PR_TRUE, nsnull);
|
|
|
|
}
|