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/. */
7 #if !defined(__nsHTTPCompressConv__h__)
8 # define __nsHTTPCompressConv__h__ 1
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"
19 class nsIStringInputStream
;
21 # define NS_HTTPCOMPRESSCONVERTER_CID \
23 /* 66230b2b-17fa-4bd3-abf4-07986151022d */ \
24 0x66230b2b, 0x17fa, 0x4bd3, { \
25 0xab, 0xf4, 0x07, 0x98, 0x61, 0x51, 0x02, 0x2d \
29 # define HTTP_DEFLATE_TYPE "deflate"
30 # define HTTP_GZIP_TYPE "gzip"
31 # define HTTP_X_GZIP_TYPE "x-gzip"
32 # define HTTP_COMPRESS_TYPE "compress"
33 # define HTTP_X_COMPRESS_TYPE "x-compress"
34 # define HTTP_BROTLI_TYPE "br"
35 # define HTTP_IDENTITY_TYPE "identity"
36 # define HTTP_UNCOMPRESSED_TYPE "uncompressed"
37 # define HTTP_ZSTD_TYPE "zstd"
38 # define HTTP_ZST_TYPE "zst"
46 class nsHTTPCompressConv
: public nsIStreamConverter
,
47 public nsICompressConvStats
{
49 // nsISupports methods
50 NS_DECL_THREADSAFE_ISUPPORTS
51 NS_DECL_NSIREQUESTOBSERVER
52 NS_DECL_NSISTREAMLISTENER
53 NS_DECL_NSICOMPRESSCONVSTATS
54 NS_DECL_NSITHREADRETARGETABLESTREAMLISTENER
56 // nsIStreamConverter methods
57 NS_DECL_NSISTREAMCONVERTER
61 using CompressMode
= enum {
63 HTTP_COMPRESS_DEFLATE
,
64 HTTP_COMPRESS_COMPRESS
,
66 HTTP_COMPRESS_IDENTITY
,
71 virtual ~nsHTTPCompressConv();
73 nsCOMPtr
<nsIStreamListener
>
74 mListener
; // this guy gets the converted data via his OnDataAvailable ()
75 Atomic
<CompressMode
, Relaxed
> mMode
{HTTP_COMPRESS_IDENTITY
};
77 unsigned char* mOutBuffer
{nullptr};
78 unsigned char* mInpBuffer
{nullptr};
80 uint32_t mOutBufferLen
{0};
81 uint32_t mInpBufferLen
{0};
83 UniquePtr
<BrotliWrapper
> mBrotli
;
84 UniquePtr
<ZstdWrapper
> mZstd
;
86 nsCOMPtr
<nsIStringInputStream
> mStream
;
88 static nsresult
BrotliHandler(nsIInputStream
* stream
, void* closure
,
89 const char* dataIn
, uint32_t, uint32_t avail
,
92 static nsresult
ZstdHandler(nsIInputStream
* stream
, void* closure
,
93 const char* dataIn
, uint32_t, uint32_t avail
,
96 nsresult
do_OnDataAvailable(nsIRequest
* request
, uint64_t aSourceOffset
,
97 const char* buffer
, uint32_t aCount
);
99 bool mCheckHeaderDone
{false};
100 Atomic
<bool> mStreamEnded
{false};
101 bool mStreamInitialized
{false};
102 bool mDummyStreamInitialised
{false};
103 bool mFailUncleanStops
;
104 bool mDispatchToMainThread
{false};
107 unsigned mLen
{0}, hMode
{0}, mSkipCount
{0}, mFlags
{0};
109 uint32_t check_header(nsIInputStream
* iStr
, uint32_t streamLen
, nsresult
* rs
);
111 Atomic
<uint32_t, Relaxed
> mDecodedDataLength
{0};
113 mutable mozilla::Mutex mMutex MOZ_UNANNOTATED
{"nsHTTPCompressConv"};
117 } // namespace mozilla