nsHTTPCompressConv.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #if !defined(__nsHTTPCompressConv__h__)
  6. #define __nsHTTPCompressConv__h__ 1
  7. #include "nsIStreamConverter.h"
  8. #include "nsICompressConvStats.h"
  9. #include "nsIThreadRetargetableStreamListener.h"
  10. #include "nsCOMPtr.h"
  11. #include "nsAutoPtr.h"
  12. #include "mozilla/Atomics.h"
  13. #include "mozilla/Mutex.h"
  14. #include "zlib.h"
  15. // brotli includes
  16. #undef assert
  17. #include "assert.h"
  18. #include "state.h"
  19. class nsIStringInputStream;
  20. #define NS_HTTPCOMPRESSCONVERTER_CID \
  21. { \
  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. namespace mozilla {
  37. namespace net {
  38. typedef enum {
  39. HTTP_COMPRESS_GZIP,
  40. HTTP_COMPRESS_DEFLATE,
  41. HTTP_COMPRESS_COMPRESS,
  42. HTTP_COMPRESS_BROTLI,
  43. HTTP_COMPRESS_IDENTITY
  44. } CompressMode;
  45. class BrotliWrapper {
  46. public:
  47. BrotliWrapper()
  48. : mTotalOut(0),
  49. mStatus(NS_OK),
  50. mBrotliStateIsStreamEnd(false),
  51. mRequest(nullptr),
  52. mContext(nullptr),
  53. mSourceOffset(0) {
  54. BrotliDecoderStateInit(&mState, 0, 0, 0);
  55. }
  56. ~BrotliWrapper() { BrotliDecoderStateCleanup(&mState); }
  57. BrotliDecoderState mState;
  58. Atomic<size_t, Relaxed> mTotalOut;
  59. nsresult mStatus;
  60. Atomic<bool, Relaxed> mBrotliStateIsStreamEnd;
  61. nsIRequest* mRequest;
  62. nsISupports* mContext;
  63. uint64_t mSourceOffset;
  64. };
  65. class nsHTTPCompressConv : public nsIStreamConverter,
  66. public nsICompressConvStats,
  67. public nsIThreadRetargetableStreamListener {
  68. public:
  69. // nsISupports methods
  70. NS_DECL_THREADSAFE_ISUPPORTS
  71. NS_DECL_NSIREQUESTOBSERVER
  72. NS_DECL_NSISTREAMLISTENER
  73. NS_DECL_NSICOMPRESSCONVSTATS
  74. NS_DECL_NSITHREADRETARGETABLESTREAMLISTENER
  75. // nsIStreamConverter methods
  76. NS_DECL_NSISTREAMCONVERTER
  77. nsHTTPCompressConv();
  78. private:
  79. virtual ~nsHTTPCompressConv();
  80. nsCOMPtr<nsIStreamListener> mListener; // this guy gets the converted data via his OnDataAvailable ()
  81. Atomic<CompressMode, Relaxed> mMode;
  82. unsigned char* mOutBuffer;
  83. unsigned char* mInpBuffer;
  84. uint32_t mOutBufferLen;
  85. uint32_t mInpBufferLen;
  86. nsAutoPtr<BrotliWrapper> mBrotli;
  87. nsCOMPtr<nsIStringInputStream> mStream;
  88. static nsresult BrotliHandler(nsIInputStream* stream,
  89. void* closure,
  90. const char* dataIn,
  91. uint32_t,
  92. uint32_t avail,
  93. uint32_t* countRead);
  94. nsresult do_OnDataAvailable(nsIRequest* request,
  95. nsISupports* aContext,
  96. uint64_t aSourceOffset,
  97. const char* buffer,
  98. uint32_t aCount);
  99. bool mCheckHeaderDone;
  100. Atomic<bool> mStreamEnded;
  101. bool mStreamInitialized;
  102. bool mDummyStreamInitialised;
  103. bool mFailUncleanStops;
  104. z_stream d_stream;
  105. unsigned mLen, hMode, mSkipCount, mFlags;
  106. uint32_t check_header(nsIInputStream* iStr, uint32_t streamLen, nsresult* rv);
  107. Atomic<uint32_t, Relaxed> mDecodedDataLength;
  108. mutable mozilla::Mutex mMutex;
  109. };
  110. } // namespace net
  111. } // namespace mozilla
  112. #endif