Thu, 15 Jan 2015 21:03:48 +0100
Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #ifndef nsUnknownDecoder_h__ |
michael@0 | 7 | #define nsUnknownDecoder_h__ |
michael@0 | 8 | |
michael@0 | 9 | #include "nsIStreamConverter.h" |
michael@0 | 10 | #include "nsIContentSniffer.h" |
michael@0 | 11 | |
michael@0 | 12 | #include "nsCOMPtr.h" |
michael@0 | 13 | #include "nsString.h" |
michael@0 | 14 | |
michael@0 | 15 | #define NS_UNKNOWNDECODER_CID \ |
michael@0 | 16 | { /* 7d7008a0-c49a-11d3-9b22-0080c7cb1080 */ \ |
michael@0 | 17 | 0x7d7008a0, \ |
michael@0 | 18 | 0xc49a, \ |
michael@0 | 19 | 0x11d3, \ |
michael@0 | 20 | {0x9b, 0x22, 0x00, 0x80, 0xc7, 0xcb, 0x10, 0x80} \ |
michael@0 | 21 | } |
michael@0 | 22 | |
michael@0 | 23 | |
michael@0 | 24 | class nsUnknownDecoder : public nsIStreamConverter, public nsIContentSniffer |
michael@0 | 25 | { |
michael@0 | 26 | public: |
michael@0 | 27 | // nsISupports methods |
michael@0 | 28 | NS_DECL_ISUPPORTS |
michael@0 | 29 | |
michael@0 | 30 | // nsIStreamConverter methods |
michael@0 | 31 | NS_DECL_NSISTREAMCONVERTER |
michael@0 | 32 | |
michael@0 | 33 | // nsIStreamListener methods |
michael@0 | 34 | NS_DECL_NSISTREAMLISTENER |
michael@0 | 35 | |
michael@0 | 36 | // nsIRequestObserver methods |
michael@0 | 37 | NS_DECL_NSIREQUESTOBSERVER |
michael@0 | 38 | |
michael@0 | 39 | // nsIContentSniffer methods |
michael@0 | 40 | NS_DECL_NSICONTENTSNIFFER |
michael@0 | 41 | |
michael@0 | 42 | nsUnknownDecoder(); |
michael@0 | 43 | |
michael@0 | 44 | protected: |
michael@0 | 45 | virtual ~nsUnknownDecoder(); |
michael@0 | 46 | |
michael@0 | 47 | virtual void DetermineContentType(nsIRequest* aRequest); |
michael@0 | 48 | nsresult FireListenerNotifications(nsIRequest* request, nsISupports *aCtxt); |
michael@0 | 49 | |
michael@0 | 50 | protected: |
michael@0 | 51 | nsCOMPtr<nsIStreamListener> mNextListener; |
michael@0 | 52 | |
michael@0 | 53 | // Function to use to check whether sniffing some potentially |
michael@0 | 54 | // dangerous types (eg HTML) is ok for this request. We can disable |
michael@0 | 55 | // sniffing for local files if needed using this. Just a security |
michael@0 | 56 | // precation thingy... who knows when we suddenly need to flip this |
michael@0 | 57 | // pref? |
michael@0 | 58 | bool AllowSniffing(nsIRequest* aRequest); |
michael@0 | 59 | |
michael@0 | 60 | // Various sniffer functions. Returning true means that a type |
michael@0 | 61 | // was determined; false means no luck. |
michael@0 | 62 | bool SniffForHTML(nsIRequest* aRequest); |
michael@0 | 63 | bool SniffForXML(nsIRequest* aRequest); |
michael@0 | 64 | |
michael@0 | 65 | // SniffURI guesses at the content type based on the URI (typically |
michael@0 | 66 | // using the extentsion) |
michael@0 | 67 | bool SniffURI(nsIRequest* aRequest); |
michael@0 | 68 | |
michael@0 | 69 | // LastDitchSniff guesses at text/plain vs. application/octet-stream |
michael@0 | 70 | // by just looking at whether the data contains null bytes, and |
michael@0 | 71 | // maybe at the fraction of chars with high bit set. Use this only |
michael@0 | 72 | // as a last-ditch attempt to decide a content type! |
michael@0 | 73 | bool LastDitchSniff(nsIRequest* aRequest); |
michael@0 | 74 | |
michael@0 | 75 | /** |
michael@0 | 76 | * An entry struct for our array of sniffers. Each entry has either |
michael@0 | 77 | * a type associated with it (set these with the SNIFFER_ENTRY macro) |
michael@0 | 78 | * or a function to be executed (set these with the |
michael@0 | 79 | * SNIFFER_ENTRY_WITH_FUNC macro). The function should take a single |
michael@0 | 80 | * nsIRequest* and returns bool -- true if it sets mContentType, |
michael@0 | 81 | * false otherwise |
michael@0 | 82 | */ |
michael@0 | 83 | struct nsSnifferEntry { |
michael@0 | 84 | typedef bool (nsUnknownDecoder::*TypeSniffFunc)(nsIRequest* aRequest); |
michael@0 | 85 | |
michael@0 | 86 | const char* mBytes; |
michael@0 | 87 | uint32_t mByteLen; |
michael@0 | 88 | |
michael@0 | 89 | // Exactly one of mMimeType and mContentTypeSniffer should be set non-null |
michael@0 | 90 | const char* mMimeType; |
michael@0 | 91 | TypeSniffFunc mContentTypeSniffer; |
michael@0 | 92 | }; |
michael@0 | 93 | |
michael@0 | 94 | #define SNIFFER_ENTRY(_bytes, _type) \ |
michael@0 | 95 | { _bytes, sizeof(_bytes) - 1, _type, nullptr } |
michael@0 | 96 | |
michael@0 | 97 | #define SNIFFER_ENTRY_WITH_FUNC(_bytes, _func) \ |
michael@0 | 98 | { _bytes, sizeof(_bytes) - 1, nullptr, _func } |
michael@0 | 99 | |
michael@0 | 100 | static nsSnifferEntry sSnifferEntries[]; |
michael@0 | 101 | static uint32_t sSnifferEntryNum; |
michael@0 | 102 | |
michael@0 | 103 | char *mBuffer; |
michael@0 | 104 | uint32_t mBufferLen; |
michael@0 | 105 | bool mRequireHTMLsuffix; |
michael@0 | 106 | |
michael@0 | 107 | nsCString mContentType; |
michael@0 | 108 | |
michael@0 | 109 | }; |
michael@0 | 110 | |
michael@0 | 111 | #define NS_BINARYDETECTOR_CID \ |
michael@0 | 112 | { /* a2027ec6-ba0d-4c72-805d-148233f5f33c */ \ |
michael@0 | 113 | 0xa2027ec6, \ |
michael@0 | 114 | 0xba0d, \ |
michael@0 | 115 | 0x4c72, \ |
michael@0 | 116 | {0x80, 0x5d, 0x14, 0x82, 0x33, 0xf5, 0xf3, 0x3c} \ |
michael@0 | 117 | } |
michael@0 | 118 | |
michael@0 | 119 | /** |
michael@0 | 120 | * Class that detects whether a data stream is text or binary. This reuses |
michael@0 | 121 | * most of nsUnknownDecoder except the actual content-type determination logic |
michael@0 | 122 | * -- our overridden DetermineContentType simply calls LastDitchSniff and sets |
michael@0 | 123 | * the type to APPLICATION_GUESS_FROM_EXT if the data is detected as binary. |
michael@0 | 124 | */ |
michael@0 | 125 | class nsBinaryDetector : public nsUnknownDecoder |
michael@0 | 126 | { |
michael@0 | 127 | protected: |
michael@0 | 128 | virtual void DetermineContentType(nsIRequest* aRequest); |
michael@0 | 129 | }; |
michael@0 | 130 | |
michael@0 | 131 | #define NS_BINARYDETECTOR_CATEGORYENTRY \ |
michael@0 | 132 | { NS_CONTENT_SNIFFER_CATEGORY, "Binary Detector", NS_BINARYDETECTOR_CONTRACTID } |
michael@0 | 133 | |
michael@0 | 134 | #endif /* nsUnknownDecoder_h__ */ |
michael@0 | 135 |