1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1999
20 * the Initial Developer. All Rights Reserved.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #ifndef nsUnknownDecoder_h__
39 #define nsUnknownDecoder_h__
41 #include "nsIStreamConverter.h"
42 #include "nsIChannel.h"
43 #include "nsIContentSniffer.h"
44 #include "nsIGenericFactory.h"
49 #define NS_UNKNOWNDECODER_CID \
50 { /* 7d7008a0-c49a-11d3-9b22-0080c7cb1080 */ \
54 {0x9b, 0x22, 0x00, 0x80, 0xc7, 0xcb, 0x10, 0x80} \
58 class nsUnknownDecoder
: public nsIStreamConverter
, public nsIContentSniffer
61 // nsISupports methods
64 // nsIStreamConverter methods
65 NS_DECL_NSISTREAMCONVERTER
67 // nsIStreamListener methods
68 NS_DECL_NSISTREAMLISTENER
70 // nsIRequestObserver methods
71 NS_DECL_NSIREQUESTOBSERVER
73 // nsIContentSniffer methods
74 NS_DECL_NSICONTENTSNIFFER
79 virtual ~nsUnknownDecoder();
81 virtual void DetermineContentType(nsIRequest
* aRequest
);
82 nsresult
FireListenerNotifications(nsIRequest
* request
, nsISupports
*aCtxt
);
85 nsCOMPtr
<nsIStreamListener
> mNextListener
;
87 // Function to use to check whether sniffing some potentially
88 // dangerous types (eg HTML) is ok for this request. We can disable
89 // sniffing for local files if needed using this. Just a security
90 // precation thingy... who knows when we suddenly need to flip this
92 PRBool
AllowSniffing(nsIRequest
* aRequest
);
94 // Various sniffer functions. Returning PR_TRUE means that a type
95 // was determined; PR_FALSE means no luck.
96 PRBool
TryContentSniffers(nsIRequest
* aRequest
);
97 PRBool
SniffForHTML(nsIRequest
* aRequest
);
98 PRBool
SniffForXML(nsIRequest
* aRequest
);
100 // SniffURI guesses at the content type based on the URI (typically
101 // using the extentsion)
102 PRBool
SniffURI(nsIRequest
* aRequest
);
104 // LastDitchSniff guesses at text/plain vs. application/octet-stream
105 // by just looking at whether the data contains null bytes, and
106 // maybe at the fraction of chars with high bit set. Use this only
107 // as a last-ditch attempt to decide a content type!
108 PRBool
LastDitchSniff(nsIRequest
* aRequest
);
111 * An entry struct for our array of sniffers. Each entry has either
112 * a type associated with it (set these with the SNIFFER_ENTRY macro)
113 * or a function to be executed (set these with the
114 * SNIFFER_ENTRY_WITH_FUNC macro). The function should take a single
115 * nsIRequest* and returns PRBool -- PR_TRUE if it sets mContentType,
118 struct nsSnifferEntry
{
119 typedef PRBool (nsUnknownDecoder::*TypeSniffFunc
)(nsIRequest
* aRequest
);
124 // Exactly one of mMimeType and mContentTypeSniffer should be set non-null
125 const char* mMimeType
;
126 TypeSniffFunc mContentTypeSniffer
;
129 #define SNIFFER_ENTRY(_bytes, _type) \
130 { _bytes, sizeof(_bytes) - 1, _type, nsnull }
132 #define SNIFFER_ENTRY_WITH_FUNC(_bytes, _func) \
133 { _bytes, sizeof(_bytes) - 1, nsnull, _func }
135 static nsSnifferEntry sSnifferEntries
[];
136 static PRUint32 sSnifferEntryNum
;
140 PRBool mRequireHTMLsuffix
;
142 nsCString mContentType
;
146 #define NS_BINARYDETECTOR_CID \
147 { /* a2027ec6-ba0d-4c72-805d-148233f5f33c */ \
151 {0x80, 0x5d, 0x14, 0x82, 0x33, 0xf5, 0xf3, 0x3c} \
155 * Class that detects whether a data stream is text or binary. This reuses
156 * most of nsUnknownDecoder except the actual content-type determination logic
157 * -- our overridden DetermineContentType simply calls LastDitchSniff and sets
158 * the type to APPLICATION_GUESS_FROM_EXT if the data is detected as binary.
160 class nsBinaryDetector
: public nsUnknownDecoder
163 static NS_METHOD
Register(nsIComponentManager
* compMgr
, nsIFile
* path
,
164 const char* registryLocation
,
165 const char* componentType
,
166 const nsModuleComponentInfo
*info
);
169 virtual void DetermineContentType(nsIRequest
* aRequest
);
172 #endif /* nsUnknownDecoder_h__ */