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"
43 class nsHTTPCompressConv
: public nsIStreamConverter
,
44 public nsICompressConvStats
{
46 // nsISupports methods
47 NS_DECL_THREADSAFE_ISUPPORTS
48 NS_DECL_NSIREQUESTOBSERVER
49 NS_DECL_NSISTREAMLISTENER
50 NS_DECL_NSICOMPRESSCONVSTATS
51 NS_DECL_NSITHREADRETARGETABLESTREAMLISTENER
53 // nsIStreamConverter methods
54 NS_DECL_NSISTREAMCONVERTER
58 using CompressMode
= enum {
60 HTTP_COMPRESS_DEFLATE
,
61 HTTP_COMPRESS_COMPRESS
,
63 HTTP_COMPRESS_IDENTITY
67 virtual ~nsHTTPCompressConv();
69 nsCOMPtr
<nsIStreamListener
>
70 mListener
; // this guy gets the converted data via his OnDataAvailable ()
71 Atomic
<CompressMode
, Relaxed
> mMode
{HTTP_COMPRESS_IDENTITY
};
73 unsigned char* mOutBuffer
{nullptr};
74 unsigned char* mInpBuffer
{nullptr};
76 uint32_t mOutBufferLen
{0};
77 uint32_t mInpBufferLen
{0};
79 UniquePtr
<BrotliWrapper
> mBrotli
;
81 nsCOMPtr
<nsIStringInputStream
> mStream
;
83 static nsresult
BrotliHandler(nsIInputStream
* stream
, void* closure
,
84 const char* dataIn
, uint32_t, uint32_t avail
,
87 nsresult
do_OnDataAvailable(nsIRequest
* request
, uint64_t aSourceOffset
,
88 const char* buffer
, uint32_t aCount
);
90 bool mCheckHeaderDone
{false};
91 Atomic
<bool> mStreamEnded
{false};
92 bool mStreamInitialized
{false};
93 bool mDummyStreamInitialised
{false};
94 bool mFailUncleanStops
;
95 bool mDispatchToMainThread
{false};
98 unsigned mLen
{0}, hMode
{0}, mSkipCount
{0}, mFlags
{0};
100 uint32_t check_header(nsIInputStream
* iStr
, uint32_t streamLen
, nsresult
* rs
);
102 Atomic
<uint32_t, Relaxed
> mDecodedDataLength
{0};
104 mutable mozilla::Mutex mMutex MOZ_UNANNOTATED
{"nsHTTPCompressConv"};
108 } // namespace mozilla