Back to index

lightning-sunbird  0.9+nobinonly
nsUnknownDecoder.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1999
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #ifndef nsUnknownDecoder_h__
00039 #define nsUnknownDecoder_h__
00040 
00041 #include "nsIStreamConverter.h"
00042 #include "nsIChannel.h"
00043 #include "nsIContentSniffer.h"
00044 
00045 #include "nsCOMPtr.h"
00046 #include "nsString.h"
00047 
00048 #define NS_UNKNOWNDECODER_CID                        \
00049 { /* 7d7008a0-c49a-11d3-9b22-0080c7cb1080 */         \
00050     0x7d7008a0,                                      \
00051     0xc49a,                                          \
00052     0x11d3,                                          \
00053     {0x9b, 0x22, 0x00, 0x80, 0xc7, 0xcb, 0x10, 0x80}       \
00054 }
00055 
00056 
00057 class nsUnknownDecoder : public nsIStreamConverter
00058                        , public nsIContentSniffer_MOZILLA_1_8_BRANCH
00059 {
00060 public:
00061   // nsISupports methods
00062   NS_DECL_ISUPPORTS
00063 
00064   // nsIStreamConverter methods
00065   NS_DECL_NSISTREAMCONVERTER
00066 
00067   // nsIStreamListener methods
00068   NS_DECL_NSISTREAMLISTENER
00069 
00070   // nsIRequestObserver methods
00071   NS_DECL_NSIREQUESTOBSERVER
00072 
00073   // nsIContentSniffer methods
00074   NS_DECL_NSICONTENTSNIFFER_MOZILLA_1_8_BRANCH
00075 
00076   nsUnknownDecoder();
00077 
00078 protected:
00079   virtual ~nsUnknownDecoder();
00080 
00081   virtual void DetermineContentType(nsIRequest* aRequest);
00082   nsresult FireListenerNotifications(nsIRequest* request, nsISupports *aCtxt);
00083 
00084 protected:
00085   nsCOMPtr<nsIStreamListener> mNextListener;
00086 
00087   // Function to use to check whether sniffing some potentially
00088   // dangerous types (eg HTML) is ok for this request.  We can disable
00089   // sniffing for local files if needed using this.  Just a security
00090   // precation thingy... who knows when we suddenly need to flip this
00091   // pref?
00092   PRBool AllowSniffing(nsIRequest* aRequest);
00093   
00094   // Various sniffer functions.  Returning PR_TRUE means that a type
00095   // was determined; PR_FALSE means no luck.
00096   PRBool TryContentSniffers(nsIRequest* aRequest);
00097   PRBool SniffForHTML(nsIRequest* aRequest);
00098   PRBool SniffForXML(nsIRequest* aRequest);
00099 
00100   // SniffURI guesses at the content type based on the URI (typically
00101   // using the extentsion)
00102   PRBool SniffURI(nsIRequest* aRequest);
00103 
00104   // LastDitchSniff guesses at text/plain vs. application/octet-stream
00105   // by just looking at whether the data contains null bytes, and
00106   // maybe at the fraction of chars with high bit set.  Use this only
00107   // as a last-ditch attempt to decide a content type!
00108   PRBool LastDitchSniff(nsIRequest* aRequest);
00109 
00118   struct nsSnifferEntry {
00119     typedef PRBool (nsUnknownDecoder::*TypeSniffFunc)(nsIRequest* aRequest);
00120     
00121     const char* mBytes;
00122     PRUint32 mByteLen;
00123     
00124     // Exactly one of mMimeType and mContentTypeSniffer should be set non-null
00125     const char* mMimeType;
00126     TypeSniffFunc mContentTypeSniffer;
00127   };
00128 
00129 #define SNIFFER_ENTRY(_bytes, _type) \
00130   { _bytes, sizeof(_bytes) - 1, _type, nsnull }
00131 
00132 #define SNIFFER_ENTRY_WITH_FUNC(_bytes, _func) \
00133   { _bytes, sizeof(_bytes) - 1, nsnull, _func }
00134 
00135   static nsSnifferEntry sSnifferEntries[];
00136   static PRUint32 sSnifferEntryNum;
00137   
00138   char *mBuffer;
00139   PRUint32 mBufferLen;
00140   PRBool mRequireHTMLsuffix;
00141 
00142   nsCString mContentType;
00143 
00144 };
00145 
00146 #define NS_BINARYDETECTOR_CID                        \
00147 { /* a2027ec6-ba0d-4c72-805d-148233f5f33c */         \
00148     0xa2027ec6,                                      \
00149     0xba0d,                                          \
00150     0x4c72,                                          \
00151     {0x80, 0x5d, 0x14, 0x82, 0x33, 0xf5, 0xf3, 0x3c} \
00152 }
00153 
00160 class nsBinaryDetector : public nsUnknownDecoder
00161 {
00162 protected:
00163   virtual void DetermineContentType(nsIRequest* aRequest);
00164 };
00165 
00166 #endif /* nsUnknownDecoder_h__ */
00167