netwerk/streamconv/converters/nsUnknownDecoder.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/netwerk/streamconv/converters/nsUnknownDecoder.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,135 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#ifndef nsUnknownDecoder_h__
    1.10 +#define nsUnknownDecoder_h__
    1.11 +
    1.12 +#include "nsIStreamConverter.h"
    1.13 +#include "nsIContentSniffer.h"
    1.14 +
    1.15 +#include "nsCOMPtr.h"
    1.16 +#include "nsString.h"
    1.17 +
    1.18 +#define NS_UNKNOWNDECODER_CID                        \
    1.19 +{ /* 7d7008a0-c49a-11d3-9b22-0080c7cb1080 */         \
    1.20 +    0x7d7008a0,                                      \
    1.21 +    0xc49a,                                          \
    1.22 +    0x11d3,                                          \
    1.23 +    {0x9b, 0x22, 0x00, 0x80, 0xc7, 0xcb, 0x10, 0x80}       \
    1.24 +}
    1.25 +
    1.26 +
    1.27 +class nsUnknownDecoder : public nsIStreamConverter, public nsIContentSniffer
    1.28 +{
    1.29 +public:
    1.30 +  // nsISupports methods
    1.31 +  NS_DECL_ISUPPORTS
    1.32 +
    1.33 +  // nsIStreamConverter methods
    1.34 +  NS_DECL_NSISTREAMCONVERTER
    1.35 +
    1.36 +  // nsIStreamListener methods
    1.37 +  NS_DECL_NSISTREAMLISTENER
    1.38 +
    1.39 +  // nsIRequestObserver methods
    1.40 +  NS_DECL_NSIREQUESTOBSERVER
    1.41 +
    1.42 +  // nsIContentSniffer methods
    1.43 +  NS_DECL_NSICONTENTSNIFFER
    1.44 +
    1.45 +  nsUnknownDecoder();
    1.46 +
    1.47 +protected:
    1.48 +  virtual ~nsUnknownDecoder();
    1.49 +
    1.50 +  virtual void DetermineContentType(nsIRequest* aRequest);
    1.51 +  nsresult FireListenerNotifications(nsIRequest* request, nsISupports *aCtxt);
    1.52 +
    1.53 +protected:
    1.54 +  nsCOMPtr<nsIStreamListener> mNextListener;
    1.55 +
    1.56 +  // Function to use to check whether sniffing some potentially
    1.57 +  // dangerous types (eg HTML) is ok for this request.  We can disable
    1.58 +  // sniffing for local files if needed using this.  Just a security
    1.59 +  // precation thingy... who knows when we suddenly need to flip this
    1.60 +  // pref?
    1.61 +  bool AllowSniffing(nsIRequest* aRequest);
    1.62 +  
    1.63 +  // Various sniffer functions.  Returning true means that a type
    1.64 +  // was determined; false means no luck.
    1.65 +  bool SniffForHTML(nsIRequest* aRequest);
    1.66 +  bool SniffForXML(nsIRequest* aRequest);
    1.67 +
    1.68 +  // SniffURI guesses at the content type based on the URI (typically
    1.69 +  // using the extentsion)
    1.70 +  bool SniffURI(nsIRequest* aRequest);
    1.71 +
    1.72 +  // LastDitchSniff guesses at text/plain vs. application/octet-stream
    1.73 +  // by just looking at whether the data contains null bytes, and
    1.74 +  // maybe at the fraction of chars with high bit set.  Use this only
    1.75 +  // as a last-ditch attempt to decide a content type!
    1.76 +  bool LastDitchSniff(nsIRequest* aRequest);
    1.77 +
    1.78 +  /**
    1.79 +   * An entry struct for our array of sniffers.  Each entry has either
    1.80 +   * a type associated with it (set these with the SNIFFER_ENTRY macro)
    1.81 +   * or a function to be executed (set these with the
    1.82 +   * SNIFFER_ENTRY_WITH_FUNC macro).  The function should take a single
    1.83 +   * nsIRequest* and returns bool -- true if it sets mContentType,
    1.84 +   * false otherwise
    1.85 +   */
    1.86 +  struct nsSnifferEntry {
    1.87 +    typedef bool (nsUnknownDecoder::*TypeSniffFunc)(nsIRequest* aRequest);
    1.88 +    
    1.89 +    const char* mBytes;
    1.90 +    uint32_t mByteLen;
    1.91 +    
    1.92 +    // Exactly one of mMimeType and mContentTypeSniffer should be set non-null
    1.93 +    const char* mMimeType;
    1.94 +    TypeSniffFunc mContentTypeSniffer;
    1.95 +  };
    1.96 +
    1.97 +#define SNIFFER_ENTRY(_bytes, _type) \
    1.98 +  { _bytes, sizeof(_bytes) - 1, _type, nullptr }
    1.99 +
   1.100 +#define SNIFFER_ENTRY_WITH_FUNC(_bytes, _func) \
   1.101 +  { _bytes, sizeof(_bytes) - 1, nullptr, _func }
   1.102 +
   1.103 +  static nsSnifferEntry sSnifferEntries[];
   1.104 +  static uint32_t sSnifferEntryNum;
   1.105 +  
   1.106 +  char *mBuffer;
   1.107 +  uint32_t mBufferLen;
   1.108 +  bool mRequireHTMLsuffix;
   1.109 +
   1.110 +  nsCString mContentType;
   1.111 +
   1.112 +};
   1.113 +
   1.114 +#define NS_BINARYDETECTOR_CID                        \
   1.115 +{ /* a2027ec6-ba0d-4c72-805d-148233f5f33c */         \
   1.116 +    0xa2027ec6,                                      \
   1.117 +    0xba0d,                                          \
   1.118 +    0x4c72,                                          \
   1.119 +    {0x80, 0x5d, 0x14, 0x82, 0x33, 0xf5, 0xf3, 0x3c} \
   1.120 +}
   1.121 +
   1.122 +/**
   1.123 + * Class that detects whether a data stream is text or binary.  This reuses
   1.124 + * most of nsUnknownDecoder except the actual content-type determination logic
   1.125 + * -- our overridden DetermineContentType simply calls LastDitchSniff and sets
   1.126 + * the type to APPLICATION_GUESS_FROM_EXT if the data is detected as binary.
   1.127 + */
   1.128 +class nsBinaryDetector : public nsUnknownDecoder
   1.129 +{
   1.130 +protected:
   1.131 +  virtual void DetermineContentType(nsIRequest* aRequest);
   1.132 +};
   1.133 +
   1.134 +#define NS_BINARYDETECTOR_CATEGORYENTRY \
   1.135 +  { NS_CONTENT_SNIFFER_CATEGORY, "Binary Detector", NS_BINARYDETECTOR_CONTRACTID }
   1.136 +
   1.137 +#endif /* nsUnknownDecoder_h__ */
   1.138 +

mercurial