2001-09-29 00:14:13 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2000-01-08 09:17:40 +03:00
|
|
|
|
|
|
|
#ifndef nsUnknownDecoder_h__
|
|
|
|
#define nsUnknownDecoder_h__
|
|
|
|
|
|
|
|
#include "nsIStreamConverter.h"
|
|
|
|
#include "nsIChannel.h"
|
2006-02-17 19:54:27 +03:00
|
|
|
#include "nsIContentSniffer.h"
|
2000-01-08 09:17:40 +03:00
|
|
|
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
|
|
|
|
#define NS_UNKNOWNDECODER_CID \
|
|
|
|
{ /* 7d7008a0-c49a-11d3-9b22-0080c7cb1080 */ \
|
|
|
|
0x7d7008a0, \
|
|
|
|
0xc49a, \
|
|
|
|
0x11d3, \
|
|
|
|
{0x9b, 0x22, 0x00, 0x80, 0xc7, 0xcb, 0x10, 0x80} \
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-02-17 19:54:27 +03:00
|
|
|
class nsUnknownDecoder : public nsIStreamConverter, public nsIContentSniffer
|
2000-01-08 09:17:40 +03:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
// nsISupports methods
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
// nsIStreamConverter methods
|
|
|
|
NS_DECL_NSISTREAMCONVERTER
|
|
|
|
|
|
|
|
// nsIStreamListener methods
|
|
|
|
NS_DECL_NSISTREAMLISTENER
|
|
|
|
|
2001-04-10 10:01:08 +04:00
|
|
|
// nsIRequestObserver methods
|
|
|
|
NS_DECL_NSIREQUESTOBSERVER
|
2000-01-08 09:17:40 +03:00
|
|
|
|
2006-02-17 19:54:27 +03:00
|
|
|
// nsIContentSniffer methods
|
|
|
|
NS_DECL_NSICONTENTSNIFFER
|
|
|
|
|
2000-01-08 09:17:40 +03:00
|
|
|
nsUnknownDecoder();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual ~nsUnknownDecoder();
|
|
|
|
|
2004-01-08 06:56:02 +03:00
|
|
|
virtual void DetermineContentType(nsIRequest* aRequest);
|
2001-02-21 23:38:08 +03:00
|
|
|
nsresult FireListenerNotifications(nsIRequest* request, nsISupports *aCtxt);
|
2000-01-08 09:17:40 +03:00
|
|
|
|
|
|
|
protected:
|
|
|
|
nsCOMPtr<nsIStreamListener> mNextListener;
|
|
|
|
|
2003-01-11 22:34:13 +03:00
|
|
|
// Function to use to check whether sniffing some potentially
|
|
|
|
// dangerous types (eg HTML) is ok for this request. We can disable
|
|
|
|
// sniffing for local files if needed using this. Just a security
|
|
|
|
// precation thingy... who knows when we suddenly need to flip this
|
|
|
|
// pref?
|
2011-09-29 10:19:26 +04:00
|
|
|
bool AllowSniffing(nsIRequest* aRequest);
|
2003-01-11 22:34:13 +03:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
// Various sniffer functions. Returning true means that a type
|
|
|
|
// was determined; false means no luck.
|
2011-09-29 10:19:26 +04:00
|
|
|
bool TryContentSniffers(nsIRequest* aRequest);
|
|
|
|
bool SniffForHTML(nsIRequest* aRequest);
|
|
|
|
bool SniffForXML(nsIRequest* aRequest);
|
2003-04-05 02:46:50 +04:00
|
|
|
|
|
|
|
// SniffURI guesses at the content type based on the URI (typically
|
|
|
|
// using the extentsion)
|
2011-09-29 10:19:26 +04:00
|
|
|
bool SniffURI(nsIRequest* aRequest);
|
2003-04-05 02:46:50 +04:00
|
|
|
|
|
|
|
// LastDitchSniff guesses at text/plain vs. application/octet-stream
|
|
|
|
// by just looking at whether the data contains null bytes, and
|
|
|
|
// maybe at the fraction of chars with high bit set. Use this only
|
|
|
|
// as a last-ditch attempt to decide a content type!
|
2011-09-29 10:19:26 +04:00
|
|
|
bool LastDitchSniff(nsIRequest* aRequest);
|
2003-01-11 22:34:13 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* An entry struct for our array of sniffers. 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
|
2011-09-29 10:19:26 +04:00
|
|
|
* nsIRequest* and returns bool -- true if it sets mContentType,
|
2011-10-17 18:59:28 +04:00
|
|
|
* false otherwise
|
2003-01-11 22:34:13 +03:00
|
|
|
*/
|
|
|
|
struct nsSnifferEntry {
|
2011-09-29 10:19:26 +04:00
|
|
|
typedef bool (nsUnknownDecoder::*TypeSniffFunc)(nsIRequest* aRequest);
|
2003-01-11 22:34:13 +03:00
|
|
|
|
|
|
|
const char* mBytes;
|
|
|
|
PRUint32 mByteLen;
|
|
|
|
|
|
|
|
// Exactly one of mMimeType and mContentTypeSniffer should be set non-null
|
|
|
|
const char* mMimeType;
|
|
|
|
TypeSniffFunc mContentTypeSniffer;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define SNIFFER_ENTRY(_bytes, _type) \
|
|
|
|
{ _bytes, sizeof(_bytes) - 1, _type, nsnull }
|
|
|
|
|
|
|
|
#define SNIFFER_ENTRY_WITH_FUNC(_bytes, _func) \
|
|
|
|
{ _bytes, sizeof(_bytes) - 1, nsnull, _func }
|
|
|
|
|
|
|
|
static nsSnifferEntry sSnifferEntries[];
|
|
|
|
static PRUint32 sSnifferEntryNum;
|
|
|
|
|
2000-01-08 09:17:40 +03:00
|
|
|
char *mBuffer;
|
|
|
|
PRUint32 mBufferLen;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mRequireHTMLsuffix;
|
2000-01-08 09:17:40 +03:00
|
|
|
|
|
|
|
nsCString mContentType;
|
2003-01-11 22:34:13 +03:00
|
|
|
|
2000-01-08 09:17:40 +03:00
|
|
|
};
|
|
|
|
|
2004-01-08 06:56:02 +03:00
|
|
|
#define NS_BINARYDETECTOR_CID \
|
|
|
|
{ /* a2027ec6-ba0d-4c72-805d-148233f5f33c */ \
|
|
|
|
0xa2027ec6, \
|
|
|
|
0xba0d, \
|
|
|
|
0x4c72, \
|
|
|
|
{0x80, 0x5d, 0x14, 0x82, 0x33, 0xf5, 0xf3, 0x3c} \
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class that detects whether a data stream is text or binary. This reuses
|
|
|
|
* most of nsUnknownDecoder except the actual content-type determination logic
|
|
|
|
* -- our overridden DetermineContentType simply calls LastDitchSniff and sets
|
|
|
|
* the type to APPLICATION_GUESS_FROM_EXT if the data is detected as binary.
|
|
|
|
*/
|
|
|
|
class nsBinaryDetector : public nsUnknownDecoder
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
virtual void DetermineContentType(nsIRequest* aRequest);
|
|
|
|
};
|
2000-01-08 09:17:40 +03:00
|
|
|
|
2010-06-10 22:11:40 +04:00
|
|
|
#define NS_BINARYDETECTOR_CATEGORYENTRY \
|
|
|
|
{ NS_CONTENT_SNIFFER_CATEGORY, "Binary Detector", NS_BINARYDETECTOR_CONTRACTID }
|
|
|
|
|
2000-01-10 08:31:26 +03:00
|
|
|
#endif /* nsUnknownDecoder_h__ */
|
|
|
|
|