/src/mozilla-central/netwerk/streamconv/converters/nsHTTPCompressConv.h
Line | Count | Source (jump to first uncovered line) |
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 "nsAutoPtr.h" |
15 | | #include "mozilla/Atomics.h" |
16 | | #include "mozilla/Mutex.h" |
17 | | |
18 | | #include "zlib.h" |
19 | | |
20 | | // brotli includes |
21 | | #undef assert |
22 | | #include "assert.h" |
23 | | #include "state.h" |
24 | | |
25 | | class nsIStringInputStream; |
26 | | |
27 | | #define NS_HTTPCOMPRESSCONVERTER_CID \ |
28 | | { \ |
29 | | /* 66230b2b-17fa-4bd3-abf4-07986151022d */ \ |
30 | | 0x66230b2b, \ |
31 | | 0x17fa, \ |
32 | | 0x4bd3, \ |
33 | | {0xab, 0xf4, 0x07, 0x98, 0x61, 0x51, 0x02, 0x2d} \ |
34 | | } |
35 | | |
36 | | |
37 | 0 | #define HTTP_DEFLATE_TYPE "deflate" |
38 | 0 | #define HTTP_GZIP_TYPE "gzip" |
39 | 0 | #define HTTP_X_GZIP_TYPE "x-gzip" |
40 | 0 | #define HTTP_COMPRESS_TYPE "compress" |
41 | 0 | #define HTTP_X_COMPRESS_TYPE "x-compress" |
42 | 0 | #define HTTP_BROTLI_TYPE "br" |
43 | | #define HTTP_IDENTITY_TYPE "identity" |
44 | | #define HTTP_UNCOMPRESSED_TYPE "uncompressed" |
45 | | |
46 | | namespace mozilla { |
47 | | namespace net { |
48 | | |
49 | | typedef enum { |
50 | | HTTP_COMPRESS_GZIP, |
51 | | HTTP_COMPRESS_DEFLATE, |
52 | | HTTP_COMPRESS_COMPRESS, |
53 | | HTTP_COMPRESS_BROTLI, |
54 | | HTTP_COMPRESS_IDENTITY |
55 | | } CompressMode; |
56 | | |
57 | | class BrotliWrapper |
58 | | { |
59 | | public: |
60 | | BrotliWrapper() |
61 | | : mTotalOut(0) |
62 | | , mStatus(NS_OK) |
63 | | , mBrotliStateIsStreamEnd(false) |
64 | | , mRequest(nullptr) |
65 | | , mContext(nullptr) |
66 | | , mSourceOffset(0) |
67 | 0 | { |
68 | 0 | BrotliDecoderStateInit(&mState, 0, 0, 0); |
69 | 0 | } |
70 | | ~BrotliWrapper() |
71 | 0 | { |
72 | 0 | BrotliDecoderStateCleanup(&mState); |
73 | 0 | } |
74 | | |
75 | | BrotliDecoderState mState; |
76 | | Atomic<size_t, Relaxed> mTotalOut; |
77 | | nsresult mStatus; |
78 | | Atomic<bool, Relaxed> mBrotliStateIsStreamEnd; |
79 | | |
80 | | nsIRequest *mRequest; |
81 | | nsISupports *mContext; |
82 | | uint64_t mSourceOffset; |
83 | | }; |
84 | | |
85 | | class nsHTTPCompressConv |
86 | | : public nsIStreamConverter |
87 | | , public nsICompressConvStats |
88 | | , public nsIThreadRetargetableStreamListener |
89 | | { |
90 | | public: |
91 | | // nsISupports methods |
92 | | NS_DECL_THREADSAFE_ISUPPORTS |
93 | | NS_DECL_NSIREQUESTOBSERVER |
94 | | NS_DECL_NSISTREAMLISTENER |
95 | | NS_DECL_NSICOMPRESSCONVSTATS |
96 | | NS_DECL_NSITHREADRETARGETABLESTREAMLISTENER |
97 | | |
98 | | // nsIStreamConverter methods |
99 | | NS_DECL_NSISTREAMCONVERTER |
100 | | |
101 | | nsHTTPCompressConv (); |
102 | | |
103 | | private: |
104 | | virtual ~nsHTTPCompressConv (); |
105 | | |
106 | | nsCOMPtr<nsIStreamListener> mListener; // this guy gets the converted data via his OnDataAvailable () |
107 | | Atomic<CompressMode, Relaxed> mMode; |
108 | | |
109 | | unsigned char *mOutBuffer; |
110 | | unsigned char *mInpBuffer; |
111 | | |
112 | | uint32_t mOutBufferLen; |
113 | | uint32_t mInpBufferLen; |
114 | | |
115 | | nsAutoPtr<BrotliWrapper> mBrotli; |
116 | | |
117 | | nsCOMPtr<nsIStringInputStream> mStream; |
118 | | |
119 | | static nsresult |
120 | | BrotliHandler(nsIInputStream *stream, void *closure, const char *dataIn, |
121 | | uint32_t, uint32_t avail, uint32_t *countRead); |
122 | | |
123 | | nsresult do_OnDataAvailable (nsIRequest *request, nsISupports *aContext, |
124 | | uint64_t aSourceOffset, const char *buffer, |
125 | | uint32_t aCount); |
126 | | |
127 | | bool mCheckHeaderDone; |
128 | | Atomic<bool> mStreamEnded; |
129 | | bool mStreamInitialized; |
130 | | bool mDummyStreamInitialised; |
131 | | bool mFailUncleanStops; |
132 | | |
133 | | z_stream d_stream; |
134 | | unsigned mLen, hMode, mSkipCount, mFlags; |
135 | | |
136 | | uint32_t check_header (nsIInputStream *iStr, uint32_t streamLen, nsresult *rv); |
137 | | |
138 | | Atomic<uint32_t, Relaxed> mDecodedDataLength; |
139 | | |
140 | | mutable mozilla::Mutex mMutex; |
141 | | }; |
142 | | |
143 | | } // namespace net |
144 | | } // namespace mozilla |
145 | | |
146 | | #endif |