2001-09-29 00:14:13 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: NPL 1.1/GPL 2.0/LGPL 2.1
|
2000-01-08 09:17:40 +03:00
|
|
|
*
|
2001-09-29 00:14:13 +04:00
|
|
|
* The contents of this file are subject to the Netscape 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/NPL/
|
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.
|
|
|
|
*
|
2001-09-29 00:14:13 +04:00
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* 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
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* 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
|
|
|
|
* use your version of this file under the terms of the NPL, indicate your
|
|
|
|
* 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
|
|
|
|
* the terms of any one of the NPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** 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"
|
|
|
|
#include "nsIPref.h"
|
2000-01-08 09:17:40 +03:00
|
|
|
|
2001-11-03 10:10:51 +03:00
|
|
|
#include "prcpucfg.h" // To get IS_LITTLE_ENDIAN / IS_BIG_ENDIAN
|
|
|
|
|
2000-01-08 09:17:40 +03:00
|
|
|
#define MAX_BUFFER_SIZE 1024
|
|
|
|
|
|
|
|
static NS_DEFINE_CID(kStreamConverterServiceCID, NS_STREAMCONVERTERSERVICE_CID);
|
2000-10-29 04:57:24 +03:00
|
|
|
static NS_DEFINE_IID(kPrefServiceCID, NS_PREF_CID);
|
2000-01-08 09:17:40 +03:00
|
|
|
|
2001-11-03 10:10:51 +03:00
|
|
|
#if defined WORDS_BIGENDIAN || defined IS_BIG_ENDIAN
|
|
|
|
#define LITTLE_TO_NATIVE16(x) ((((x) & 0xFF) << 8) | ((x) >> 8))
|
|
|
|
#else
|
|
|
|
#define LITTLE_TO_NATIVE16(x) x
|
|
|
|
#endif
|
2000-01-08 09:17:40 +03:00
|
|
|
|
|
|
|
nsUnknownDecoder::nsUnknownDecoder()
|
|
|
|
{
|
|
|
|
NS_INIT_ISUPPORTS();
|
|
|
|
|
|
|
|
mBuffer = nsnull;
|
|
|
|
mBufferLen = 0;
|
2000-10-29 04:57:24 +03:00
|
|
|
mRequireHTMLsuffix = PR_FALSE;
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIPref> pPrefService = do_GetService(kPrefServiceCID, &rv);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = pPrefService->GetBoolPref("security.requireHTMLsuffix", &mRequireHTMLsuffix);
|
|
|
|
}
|
|
|
|
|
2000-01-08 09:17:40 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
nsUnknownDecoder::~nsUnknownDecoder()
|
|
|
|
{
|
|
|
|
if (mBuffer) {
|
|
|
|
delete [] mBuffer;
|
|
|
|
mBuffer = nsnull;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----
|
|
|
|
//
|
|
|
|
// nsISupports implementation...
|
|
|
|
//
|
|
|
|
// ----
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF(nsUnknownDecoder);
|
|
|
|
NS_IMPL_RELEASE(nsUnknownDecoder);
|
|
|
|
|
|
|
|
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)
|
2000-01-08 09:17:40 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
|
|
|
|
// ----
|
|
|
|
//
|
|
|
|
// nsIStreamConverter methods...
|
|
|
|
//
|
|
|
|
// ----
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsUnknownDecoder::Convert(nsIInputStream *aFromStream,
|
|
|
|
const PRUnichar *aFromType,
|
|
|
|
const PRUnichar *aToType,
|
|
|
|
nsISupports *aCtxt,
|
|
|
|
nsIInputStream **aResultStream)
|
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsUnknownDecoder::AsyncConvertData(const PRUnichar *aFromType,
|
|
|
|
const PRUnichar *aToType,
|
|
|
|
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
|
|
|
|
|
|
|
NS_ASSERTION(!mContentType.IsEmpty(),
|
|
|
|
"Content type should be known by now.");
|
2001-02-21 23:38:08 +03:00
|
|
|
rv = FireListenerNotifications(request, aCtxt);
|
2000-01-08 09:17:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aCount) {
|
|
|
|
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
|
|
|
|
|
|
|
NS_ASSERTION(!mContentType.IsEmpty(),
|
|
|
|
"Content type should be known by now.");
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-02-21 23:38:08 +03:00
|
|
|
void nsUnknownDecoder::DetermineContentType(nsIRequest* request)
|
2000-01-08 09:17:40 +03:00
|
|
|
{
|
|
|
|
PRUint32 i;
|
|
|
|
|
|
|
|
NS_ASSERTION(mContentType.IsEmpty(), "Content type is already known.");
|
|
|
|
if (!mContentType.IsEmpty()) return;
|
|
|
|
|
2000-06-16 10:15:13 +04:00
|
|
|
CBufDescriptor bufDesc((const char*)mBuffer, PR_TRUE, mBufferLen, mBufferLen);
|
|
|
|
nsCAutoString str(bufDesc);
|
2000-01-08 09:17:40 +03:00
|
|
|
|
2001-11-19 23:25:53 +03:00
|
|
|
NS_NAMED_LITERAL_CSTRING(pdfStart, "%PDF-");
|
|
|
|
NS_NAMED_LITERAL_CSTRING(psStart1, "%!PS-Adobe-");
|
|
|
|
NS_NAMED_LITERAL_CSTRING(psStart2, "%! PS-Adobe-");
|
|
|
|
|
|
|
|
//
|
|
|
|
// If the buffer begins with "%PDF-" it's PDF
|
|
|
|
//
|
|
|
|
if (Substring(str, 0, pdfStart.Length()) == pdfStart) {
|
|
|
|
mContentType = APPLICATION_PDF;
|
|
|
|
}
|
|
|
|
//
|
|
|
|
// If the buffer begins with "%!PS-Adobe-" or "%! PS-Adobe-" it's PostScript
|
|
|
|
//
|
|
|
|
else if (Substring(str, 0, psStart1.Length()) == psStart1 ||
|
|
|
|
Substring(str, 0, psStart2.Length()) == psStart2) {
|
|
|
|
mContentType = APPLICATION_POSTSCRIPT;
|
|
|
|
}
|
2000-06-16 10:15:13 +04:00
|
|
|
//
|
|
|
|
// If the buffer begins with "#!" or "%!" then it is a script of some
|
|
|
|
// sort...
|
|
|
|
//
|
|
|
|
// This false match happened all the time... For example, CGI scripts
|
|
|
|
// written in sh or perl that emit HTML.
|
|
|
|
//
|
2001-11-19 23:25:53 +03:00
|
|
|
else if (str.EqualsWithConversion("#!", PR_FALSE, 2) ||
|
|
|
|
str.EqualsWithConversion("%!", PR_FALSE, 2)) {
|
2000-06-16 10:15:13 +04:00
|
|
|
mContentType = TEXT_PLAIN;
|
|
|
|
}
|
|
|
|
//
|
|
|
|
// If the buffer begins with a mailbox delimiter then it is not HTML
|
|
|
|
//
|
|
|
|
else if (str.EqualsWithConversion("From ", PR_TRUE, 5) ||
|
|
|
|
str.EqualsWithConversion(">From ", PR_TRUE, 6)) {
|
|
|
|
mContentType = TEXT_PLAIN;
|
2000-01-08 09:17:40 +03:00
|
|
|
}
|
|
|
|
//
|
2000-06-16 10:15:13 +04:00
|
|
|
// If the buffer contains "common" HTML tags then lets call it HTML :-)
|
2000-01-08 09:17:40 +03:00
|
|
|
//
|
2000-06-16 10:15:13 +04:00
|
|
|
else {
|
2000-01-08 09:17:40 +03:00
|
|
|
|
2000-10-29 04:57:24 +03:00
|
|
|
/*
|
|
|
|
* To prevent a possible attack, we will not consider this to be html
|
|
|
|
* content if it comes from the local file system
|
|
|
|
*/
|
|
|
|
|
|
|
|
PRBool isLocalFile = PR_FALSE;
|
2001-02-21 23:38:08 +03:00
|
|
|
if (request) {
|
|
|
|
nsCOMPtr<nsIChannel> aChannel = do_QueryInterface(request);
|
|
|
|
if (!request) { NS_WARNING("QI failed"); return; }
|
|
|
|
|
2000-10-29 04:57:24 +03:00
|
|
|
nsCOMPtr<nsIURI> pURL;
|
2001-01-31 04:33:03 +03:00
|
|
|
if (NS_SUCCEEDED(aChannel->GetURI(getter_AddRefs(pURL))))
|
2001-03-13 05:01:07 +03:00
|
|
|
pURL->SchemeIs("file", &isLocalFile);
|
2000-10-29 04:57:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!mRequireHTMLsuffix || !isLocalFile) {
|
|
|
|
PRInt32 offset;
|
|
|
|
|
|
|
|
offset = str.Find("<HTML", PR_TRUE);
|
2000-01-08 09:17:40 +03:00
|
|
|
if (offset < 0) {
|
2000-10-29 04:57:24 +03:00
|
|
|
offset = str.Find("<TITLE", PR_TRUE);
|
2000-01-08 09:17:40 +03:00
|
|
|
if (offset < 0) {
|
2000-10-29 04:57:24 +03:00
|
|
|
offset = str.Find("<FRAMESET", PR_TRUE);
|
2000-10-12 02:53:18 +04:00
|
|
|
if (offset < 0) {
|
2000-10-29 04:57:24 +03:00
|
|
|
offset = str.Find("<SCRIPT", PR_TRUE);
|
|
|
|
if (offset < 0) {
|
|
|
|
offset = str.Find("<BODY", PR_TRUE);
|
2001-07-24 01:47:02 +04:00
|
|
|
if (offset < 0) {
|
|
|
|
offset = str.Find("<A HREF", PR_TRUE);
|
2001-08-02 04:54:30 +04:00
|
|
|
if (offset < 0) {
|
|
|
|
offset = str.Find("<APPLET", PR_TRUE);
|
|
|
|
}
|
2001-07-24 01:47:02 +04:00
|
|
|
}
|
2000-10-29 04:57:24 +03:00
|
|
|
}
|
2000-10-12 02:53:18 +04:00
|
|
|
}
|
2000-01-08 09:17:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-10-29 04:57:24 +03:00
|
|
|
if (offset >= 0) {
|
|
|
|
mContentType = TEXT_HTML;
|
|
|
|
}
|
2000-06-16 10:15:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-06-14 01:24:11 +04:00
|
|
|
if (mContentType.IsEmpty()) {
|
|
|
|
// sniff the first couple bytes to see if we are dealing with an image
|
|
|
|
// Unlike html, it is safe to sniff for image content types for local files so
|
|
|
|
// we don't need to check for isLocalFile here..
|
|
|
|
SniffForImageMimeType((const char*)mBuffer, mBufferLen);
|
|
|
|
}
|
2000-06-16 10:15:13 +04:00
|
|
|
//
|
|
|
|
// See if the buffer has any embedded nulls. If not, then lets just
|
|
|
|
// call it text/plain...
|
|
|
|
//
|
|
|
|
if (mContentType.IsEmpty()) {
|
|
|
|
for (i=0; i<mBufferLen && mBuffer[i]; i++);
|
|
|
|
if (i == mBufferLen) {
|
|
|
|
mContentType = TEXT_PLAIN;
|
2000-01-08 09:17:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// If the buffer is not text, then just call it application/octet-stream
|
|
|
|
//
|
|
|
|
if (mContentType.IsEmpty()) {
|
2000-02-09 08:07:48 +03:00
|
|
|
mContentType = APPLICATION_OCTET_STREAM;
|
2000-01-08 09:17:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-06-14 01:24:11 +04:00
|
|
|
// the following routine was ripped directly from some code in libpr0n...
|
|
|
|
void nsUnknownDecoder::SniffForImageMimeType(const char *buf, PRUint32 len)
|
|
|
|
{
|
|
|
|
/* Is it a GIF? */
|
|
|
|
if (len >= 4 && !nsCRT::strncmp(buf, "GIF8", 4)) {
|
|
|
|
mContentType = NS_LITERAL_CSTRING("image/gif");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* or a PNG? */
|
|
|
|
if (len >= 4 && ((unsigned char)buf[0]==0x89 &&
|
|
|
|
(unsigned char)buf[1]==0x50 &&
|
|
|
|
(unsigned char)buf[2]==0x4E &&
|
|
|
|
(unsigned char)buf[3]==0x47))
|
|
|
|
{
|
|
|
|
mContentType = NS_LITERAL_CSTRING("image/png");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* maybe a JPEG (JFIF)? */
|
|
|
|
/* JFIF files start with SOI APP0 but older files can start with SOI DQT
|
|
|
|
* so we test for SOI followed by any marker, i.e. FF D8 FF
|
|
|
|
* this will also work for SPIFF JPEG files if they appear in the future.
|
|
|
|
*
|
|
|
|
* (JFIF is 0XFF 0XD8 0XFF 0XE0 <skip 2> 0X4A 0X46 0X49 0X46 0X00)
|
|
|
|
*/
|
|
|
|
if (len >= 3 &&
|
|
|
|
((unsigned char)buf[0])==0xFF &&
|
|
|
|
((unsigned char)buf[1])==0xD8 &&
|
|
|
|
((unsigned char)buf[2])==0xFF)
|
|
|
|
{
|
|
|
|
mContentType = NS_LITERAL_CSTRING("image/jpeg");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* or how about ART? */
|
|
|
|
/* ART begins with JG (4A 47). Major version offset 2.
|
|
|
|
* Minor version offset 3. Offset 4 must be NULL.
|
|
|
|
*/
|
|
|
|
if (len >= 5 &&
|
|
|
|
((unsigned char) buf[0])==0x4a &&
|
|
|
|
((unsigned char) buf[1])==0x47 &&
|
|
|
|
((unsigned char) buf[4])==0x00 )
|
|
|
|
{
|
|
|
|
mContentType = NS_LITERAL_CSTRING("image/x-jg");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2001-11-03 10:10:51 +03:00
|
|
|
if (len >= 2 && !nsCRT::strncmp(buf, "BM", 2)) {
|
|
|
|
mContentType = NS_LITERAL_CSTRING("image/bmp");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ICOs always begin with a 2-byte 0 followed by a 2-byte 1.
|
|
|
|
if (len >= 4 && !nsCRT::memcmp(buf, "\000\000\001\000", 4)) {
|
|
|
|
mContentType = NS_LITERAL_CSTRING("image/x-icon");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2001-06-14 01:24:11 +04:00
|
|
|
/* none of the above? I give up */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (!mBuffer) return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
2001-02-21 23:38:08 +03:00
|
|
|
nsCOMPtr<nsIChannel> channel = do_QueryInterface(request, &rv);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2000-01-08 09:17:40 +03:00
|
|
|
// Set the new content type on the channel...
|
2001-10-25 03:47:43 +04:00
|
|
|
rv = channel->SetContentType(mContentType.get());
|
2001-05-19 01:02:01 +04:00
|
|
|
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "Unable to set content type on channel!");
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
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
|
|
|
|
|
|
|
// 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 {
|
|
|
|
NS_ASSERTION(0, "Unable to write all the data into the pipe.");
|
|
|
|
rv = NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
delete [] mBuffer;
|
|
|
|
mBuffer = nsnull;
|
|
|
|
mBufferLen = 0;
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|