tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

nsHTTPCompressConv.h (3722B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set sw=2 ts=8 et tw=80 : */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #if !defined(__nsHTTPCompressConv__h__)
      8 #  define __nsHTTPCompressConv__h__ 1
      9 
     10 #  include "nsIStreamConverter.h"
     11 #  include "nsICompressConvStats.h"
     12 #  include "nsIThreadRetargetableStreamListener.h"
     13 #  include "nsCOMPtr.h"
     14 #  include "mozilla/Atomics.h"
     15 #  include "mozilla/Mutex.h"
     16 
     17 #  include "zlib.h"
     18 
     19 class nsIStringInputStream;
     20 
     21 #  define NS_HTTPCOMPRESSCONVERTER_CID          \
     22    {/* 66230b2b-17fa-4bd3-abf4-07986151022d */ \
     23     0x66230b2b,                                \
     24     0x17fa,                                    \
     25     0x4bd3,                                    \
     26     {0xab, 0xf4, 0x07, 0x98, 0x61, 0x51, 0x02, 0x2d}}
     27 
     28 #  define HTTP_DEFLATE_TYPE "deflate"
     29 #  define HTTP_GZIP_TYPE "gzip"
     30 #  define HTTP_X_GZIP_TYPE "x-gzip"
     31 #  define HTTP_COMPRESS_TYPE "compress"
     32 #  define HTTP_X_COMPRESS_TYPE "x-compress"
     33 #  define HTTP_BROTLI_TYPE "br"
     34 #  define HTTP_IDENTITY_TYPE "identity"
     35 #  define HTTP_UNCOMPRESSED_TYPE "uncompressed"
     36 #  define HTTP_ZSTD_TYPE "zstd"
     37 #  define HTTP_ZST_TYPE "zst"
     38 #  define HTTP_BROTLI_DICTIONARY_TYPE "dcb"
     39 #  define HTTP_ZSTD_DICTIONARY_TYPE "dcz"
     40 
     41 namespace mozilla {
     42 namespace net {
     43 
     44 class BrotliWrapper;
     45 class ZstdWrapper;
     46 
     47 class nsHTTPCompressConv : public nsIStreamConverter,
     48                           public nsICompressConvStats {
     49 public:
     50  // nsISupports methods
     51  NS_DECL_THREADSAFE_ISUPPORTS
     52  NS_DECL_NSIREQUESTOBSERVER
     53  NS_DECL_NSISTREAMLISTENER
     54  NS_DECL_NSICOMPRESSCONVSTATS
     55  NS_DECL_NSITHREADRETARGETABLESTREAMLISTENER
     56 
     57  // nsIStreamConverter methods
     58  NS_DECL_NSISTREAMCONVERTER
     59 
     60  nsHTTPCompressConv();
     61 
     62  using CompressMode = enum {
     63    HTTP_COMPRESS_GZIP,
     64    HTTP_COMPRESS_DEFLATE,
     65    HTTP_COMPRESS_COMPRESS,
     66    HTTP_COMPRESS_BROTLI,
     67    HTTP_COMPRESS_IDENTITY,
     68    HTTP_COMPRESS_ZSTD,
     69    HTTP_COMPRESS_BROTLI_DICTIONARY,
     70    HTTP_COMPRESS_ZSTD_DICTIONARY,
     71  };
     72 
     73 private:
     74  virtual ~nsHTTPCompressConv();
     75 
     76  nsCOMPtr<nsIStreamListener>
     77      mListener;  // this guy gets the converted data via his OnDataAvailable ()
     78  Atomic<CompressMode, Relaxed> mMode{HTTP_COMPRESS_IDENTITY};
     79 
     80  unsigned char* mOutBuffer{nullptr};
     81  unsigned char* mInpBuffer{nullptr};
     82 
     83  uint32_t mOutBufferLen{0};
     84  uint32_t mInpBufferLen{0};
     85 
     86  UniquePtr<BrotliWrapper> mBrotli;
     87  UniquePtr<ZstdWrapper> mZstd;
     88 
     89  nsCOMPtr<nsIStringInputStream> mStream;
     90 
     91  static nsresult BrotliHandler(nsIInputStream* stream, void* closure,
     92                                const char* dataIn, uint32_t, uint32_t avail,
     93                                uint32_t* countRead);
     94 
     95  static nsresult ZstdHandler(nsIInputStream* stream, void* closure,
     96                              const char* dataIn, uint32_t, uint32_t avail,
     97                              uint32_t* countRead);
     98 
     99  nsresult do_OnDataAvailable(nsIRequest* request, uint64_t aSourceOffset,
    100                              const char* buffer, uint32_t aCount);
    101 
    102  bool mCheckHeaderDone{false};
    103  Atomic<bool> mStreamEnded{false};
    104  bool mStreamInitialized{false};
    105  bool mDummyStreamInitialised{false};
    106  bool mFailUncleanStops;
    107  bool mDispatchToMainThread{false};
    108 
    109  z_stream d_stream{};
    110  unsigned mLen{0}, hMode{0}, mSkipCount{0}, mFlags{0};
    111 
    112  uint32_t check_header(nsIInputStream* iStr, uint32_t streamLen, nsresult* rs);
    113 
    114  Atomic<uint32_t, Relaxed> mDecodedDataLength{0};
    115 
    116  mutable mozilla::Mutex mMutex MOZ_UNANNOTATED{"nsHTTPCompressConv"};
    117 };
    118 
    119 }  // namespace net
    120 }  // namespace mozilla
    121 
    122 #endif