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
4 : *
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/
9 : *
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
13 : * License.
14 : *
15 : * The Original Code is mozilla.org code.
16 : *
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.
21 : *
22 : * Contributor(s):
23 : *
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.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #ifndef nsUnknownDecoder_h__
39 : #define nsUnknownDecoder_h__
40 :
41 : #include "nsIStreamConverter.h"
42 : #include "nsIChannel.h"
43 : #include "nsIContentSniffer.h"
44 :
45 : #include "nsCOMPtr.h"
46 : #include "nsString.h"
47 :
48 : #define NS_UNKNOWNDECODER_CID \
49 : { /* 7d7008a0-c49a-11d3-9b22-0080c7cb1080 */ \
50 : 0x7d7008a0, \
51 : 0xc49a, \
52 : 0x11d3, \
53 : {0x9b, 0x22, 0x00, 0x80, 0xc7, 0xcb, 0x10, 0x80} \
54 : }
55 :
56 :
57 : class nsUnknownDecoder : public nsIStreamConverter, public nsIContentSniffer
58 : {
59 : public:
60 : // nsISupports methods
61 : NS_DECL_ISUPPORTS
62 :
63 : // nsIStreamConverter methods
64 : NS_DECL_NSISTREAMCONVERTER
65 :
66 : // nsIStreamListener methods
67 : NS_DECL_NSISTREAMLISTENER
68 :
69 : // nsIRequestObserver methods
70 : NS_DECL_NSIREQUESTOBSERVER
71 :
72 : // nsIContentSniffer methods
73 : NS_DECL_NSICONTENTSNIFFER
74 :
75 : nsUnknownDecoder();
76 :
77 : protected:
78 : virtual ~nsUnknownDecoder();
79 :
80 : virtual void DetermineContentType(nsIRequest* aRequest);
81 : nsresult FireListenerNotifications(nsIRequest* request, nsISupports *aCtxt);
82 :
83 : protected:
84 : nsCOMPtr<nsIStreamListener> mNextListener;
85 :
86 : // Function to use to check whether sniffing some potentially
87 : // dangerous types (eg HTML) is ok for this request. We can disable
88 : // sniffing for local files if needed using this. Just a security
89 : // precation thingy... who knows when we suddenly need to flip this
90 : // pref?
91 : bool AllowSniffing(nsIRequest* aRequest);
92 :
93 : // Various sniffer functions. Returning true means that a type
94 : // was determined; false means no luck.
95 : bool TryContentSniffers(nsIRequest* aRequest);
96 : bool SniffForHTML(nsIRequest* aRequest);
97 : bool SniffForXML(nsIRequest* aRequest);
98 :
99 : // SniffURI guesses at the content type based on the URI (typically
100 : // using the extentsion)
101 : bool SniffURI(nsIRequest* aRequest);
102 :
103 : // LastDitchSniff guesses at text/plain vs. application/octet-stream
104 : // by just looking at whether the data contains null bytes, and
105 : // maybe at the fraction of chars with high bit set. Use this only
106 : // as a last-ditch attempt to decide a content type!
107 : bool LastDitchSniff(nsIRequest* aRequest);
108 :
109 : /**
110 : * An entry struct for our array of sniffers. Each entry has either
111 : * a type associated with it (set these with the SNIFFER_ENTRY macro)
112 : * or a function to be executed (set these with the
113 : * SNIFFER_ENTRY_WITH_FUNC macro). The function should take a single
114 : * nsIRequest* and returns bool -- true if it sets mContentType,
115 : * false otherwise
116 : */
117 : struct nsSnifferEntry {
118 : typedef bool (nsUnknownDecoder::*TypeSniffFunc)(nsIRequest* aRequest);
119 :
120 : const char* mBytes;
121 : PRUint32 mByteLen;
122 :
123 : // Exactly one of mMimeType and mContentTypeSniffer should be set non-null
124 : const char* mMimeType;
125 : TypeSniffFunc mContentTypeSniffer;
126 : };
127 :
128 : #define SNIFFER_ENTRY(_bytes, _type) \
129 : { _bytes, sizeof(_bytes) - 1, _type, nsnull }
130 :
131 : #define SNIFFER_ENTRY_WITH_FUNC(_bytes, _func) \
132 : { _bytes, sizeof(_bytes) - 1, nsnull, _func }
133 :
134 : static nsSnifferEntry sSnifferEntries[];
135 : static PRUint32 sSnifferEntryNum;
136 :
137 : char *mBuffer;
138 : PRUint32 mBufferLen;
139 : bool mRequireHTMLsuffix;
140 :
141 : nsCString mContentType;
142 :
143 : };
144 :
145 : #define NS_BINARYDETECTOR_CID \
146 : { /* a2027ec6-ba0d-4c72-805d-148233f5f33c */ \
147 : 0xa2027ec6, \
148 : 0xba0d, \
149 : 0x4c72, \
150 : {0x80, 0x5d, 0x14, 0x82, 0x33, 0xf5, 0xf3, 0x3c} \
151 : }
152 :
153 : /**
154 : * Class that detects whether a data stream is text or binary. This reuses
155 : * most of nsUnknownDecoder except the actual content-type determination logic
156 : * -- our overridden DetermineContentType simply calls LastDitchSniff and sets
157 : * the type to APPLICATION_GUESS_FROM_EXT if the data is detected as binary.
158 : */
159 : class nsBinaryDetector : public nsUnknownDecoder
160 5 : {
161 : protected:
162 : virtual void DetermineContentType(nsIRequest* aRequest);
163 : };
164 :
165 : #define NS_BINARYDETECTOR_CATEGORYENTRY \
166 : { NS_CONTENT_SNIFFER_CATEGORY, "Binary Detector", NS_BINARYDETECTOR_CONTRACTID }
167 :
168 : #endif /* nsUnknownDecoder_h__ */
169 :
|