Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #ifndef mozilla_net_Http2Compression_Internal_h |
michael@0 | 7 | #define mozilla_net_Http2Compression_Internal_h |
michael@0 | 8 | |
michael@0 | 9 | // HPACK |
michael@0 | 10 | // tools.ietf.org/html/draft-ietf-httpbis-header-compression-04 |
michael@0 | 11 | |
michael@0 | 12 | #include "mozilla/Attributes.h" |
michael@0 | 13 | #include "nsDeque.h" |
michael@0 | 14 | #include "nsString.h" |
michael@0 | 15 | |
michael@0 | 16 | namespace mozilla { |
michael@0 | 17 | namespace net { |
michael@0 | 18 | |
michael@0 | 19 | struct HuffmanIncomingTable; |
michael@0 | 20 | |
michael@0 | 21 | void Http2CompressionCleanup(); |
michael@0 | 22 | |
michael@0 | 23 | class nvPair |
michael@0 | 24 | { |
michael@0 | 25 | public: |
michael@0 | 26 | nvPair(const nsACString &name, const nsACString &value) |
michael@0 | 27 | : mName(name) |
michael@0 | 28 | , mValue(value) |
michael@0 | 29 | { } |
michael@0 | 30 | |
michael@0 | 31 | uint32_t Size() const { return mName.Length() + mValue.Length() + 32; } |
michael@0 | 32 | |
michael@0 | 33 | nsCString mName; |
michael@0 | 34 | nsCString mValue; |
michael@0 | 35 | }; |
michael@0 | 36 | |
michael@0 | 37 | class nvFIFO |
michael@0 | 38 | { |
michael@0 | 39 | public: |
michael@0 | 40 | nvFIFO(); |
michael@0 | 41 | ~nvFIFO(); |
michael@0 | 42 | void AddElement(const nsCString &name, const nsCString &value); |
michael@0 | 43 | void AddElement(const nsCString &name); |
michael@0 | 44 | void RemoveElement(); |
michael@0 | 45 | uint32_t ByteCount() const; |
michael@0 | 46 | uint32_t Length() const; |
michael@0 | 47 | uint32_t VariableLength() const; |
michael@0 | 48 | void Clear(); |
michael@0 | 49 | const nvPair *operator[] (int32_t index) const; |
michael@0 | 50 | |
michael@0 | 51 | private: |
michael@0 | 52 | uint32_t mByteCount; |
michael@0 | 53 | nsDeque mTable; |
michael@0 | 54 | }; |
michael@0 | 55 | |
michael@0 | 56 | class Http2BaseCompressor |
michael@0 | 57 | { |
michael@0 | 58 | public: |
michael@0 | 59 | Http2BaseCompressor(); |
michael@0 | 60 | virtual ~Http2BaseCompressor() { }; |
michael@0 | 61 | |
michael@0 | 62 | protected: |
michael@0 | 63 | // this will become a HTTP/2 SETTINGS value in a future draft |
michael@0 | 64 | const static uint32_t kDefaultMaxBuffer = 4096; |
michael@0 | 65 | |
michael@0 | 66 | virtual void ClearHeaderTable(); |
michael@0 | 67 | virtual void UpdateReferenceSet(int32_t delta); |
michael@0 | 68 | virtual void IncrementReferenceSetIndices(); |
michael@0 | 69 | virtual void MakeRoom(uint32_t amount) = 0; |
michael@0 | 70 | |
michael@0 | 71 | nsAutoTArray<uint32_t, 64> mReferenceSet; // list of indicies |
michael@0 | 72 | |
michael@0 | 73 | // the alternate set is used to track the emitted headers when |
michael@0 | 74 | // processing input for a header set. The input to the compressor |
michael@0 | 75 | // is a series of nvpairs, the input to the decompressor is the |
michael@0 | 76 | // series of op codes that make up the header block. |
michael@0 | 77 | // |
michael@0 | 78 | // after processing the input the compressor compares the alternate |
michael@0 | 79 | // set to the inherited reference set and generates indicies to |
michael@0 | 80 | // toggle off any members of alternate - inherited. the alternate |
michael@0 | 81 | // then becomes the inherited set for the next header set. |
michael@0 | 82 | // |
michael@0 | 83 | // after processing the input the decompressor comapres the alternate |
michael@0 | 84 | // set to the inherited reference set and generates headers for |
michael@0 | 85 | // anything implicit in reference - alternate. |
michael@0 | 86 | nsAutoTArray<uint32_t, 64> mAlternateReferenceSet; // list of indicies |
michael@0 | 87 | |
michael@0 | 88 | nsACString *mOutput; |
michael@0 | 89 | nvFIFO mHeaderTable; |
michael@0 | 90 | |
michael@0 | 91 | uint32_t mMaxBuffer; |
michael@0 | 92 | }; |
michael@0 | 93 | |
michael@0 | 94 | class Http2Compressor; |
michael@0 | 95 | |
michael@0 | 96 | class Http2Decompressor MOZ_FINAL : public Http2BaseCompressor |
michael@0 | 97 | { |
michael@0 | 98 | public: |
michael@0 | 99 | Http2Decompressor() { }; |
michael@0 | 100 | virtual ~Http2Decompressor() { } ; |
michael@0 | 101 | |
michael@0 | 102 | // NS_OK: Produces the working set of HTTP/1 formatted headers |
michael@0 | 103 | nsresult DecodeHeaderBlock(const uint8_t *data, uint32_t datalen, |
michael@0 | 104 | nsACString &output); |
michael@0 | 105 | |
michael@0 | 106 | void GetStatus(nsACString &hdr) { hdr = mHeaderStatus; } |
michael@0 | 107 | void GetHost(nsACString &hdr) { hdr = mHeaderHost; } |
michael@0 | 108 | void GetScheme(nsACString &hdr) { hdr = mHeaderScheme; } |
michael@0 | 109 | void GetPath(nsACString &hdr) { hdr = mHeaderPath; } |
michael@0 | 110 | void GetMethod(nsACString &hdr) { hdr = mHeaderMethod; } |
michael@0 | 111 | void SetCompressor(Http2Compressor *compressor) { mCompressor = compressor; } |
michael@0 | 112 | |
michael@0 | 113 | protected: |
michael@0 | 114 | virtual void MakeRoom(uint32_t amount) MOZ_OVERRIDE; |
michael@0 | 115 | |
michael@0 | 116 | private: |
michael@0 | 117 | nsresult DoIndexed(); |
michael@0 | 118 | nsresult DoLiteralWithoutIndex(); |
michael@0 | 119 | nsresult DoLiteralWithIncremental(); |
michael@0 | 120 | nsresult DoLiteralInternal(nsACString &, nsACString &); |
michael@0 | 121 | |
michael@0 | 122 | nsresult DecodeInteger(uint32_t prefixLen, uint32_t &result); |
michael@0 | 123 | nsresult OutputHeader(uint32_t index); |
michael@0 | 124 | nsresult OutputHeader(const nsACString &name, const nsACString &value); |
michael@0 | 125 | |
michael@0 | 126 | nsresult CopyHeaderString(uint32_t index, nsACString &name); |
michael@0 | 127 | nsresult CopyStringFromInput(uint32_t index, nsACString &val); |
michael@0 | 128 | uint8_t ExtractByte(uint8_t bitsLeft, uint32_t &bytesConsumed); |
michael@0 | 129 | nsresult CopyHuffmanStringFromInput(uint32_t index, nsACString &val); |
michael@0 | 130 | nsresult DecodeHuffmanCharacter(HuffmanIncomingTable *table, uint8_t &c, |
michael@0 | 131 | uint32_t &bytesConsumed, uint8_t &bitsLeft); |
michael@0 | 132 | nsresult DecodeFinalHuffmanCharacter(HuffmanIncomingTable *table, uint8_t &c, |
michael@0 | 133 | uint8_t &bitsLeft); |
michael@0 | 134 | |
michael@0 | 135 | Http2Compressor *mCompressor; |
michael@0 | 136 | |
michael@0 | 137 | nsCString mHeaderStatus; |
michael@0 | 138 | nsCString mHeaderHost; |
michael@0 | 139 | nsCString mHeaderScheme; |
michael@0 | 140 | nsCString mHeaderPath; |
michael@0 | 141 | nsCString mHeaderMethod; |
michael@0 | 142 | |
michael@0 | 143 | // state variables when DecodeBlock() is on the stack |
michael@0 | 144 | uint32_t mOffset; |
michael@0 | 145 | const uint8_t *mData; |
michael@0 | 146 | uint32_t mDataLen; |
michael@0 | 147 | }; |
michael@0 | 148 | |
michael@0 | 149 | |
michael@0 | 150 | class Http2Compressor MOZ_FINAL : public Http2BaseCompressor |
michael@0 | 151 | { |
michael@0 | 152 | public: |
michael@0 | 153 | Http2Compressor() : mParsedContentLength(-1) { }; |
michael@0 | 154 | virtual ~Http2Compressor() { } |
michael@0 | 155 | |
michael@0 | 156 | // HTTP/1 formatted header block as input - HTTP/2 formatted |
michael@0 | 157 | // header block as output |
michael@0 | 158 | nsresult EncodeHeaderBlock(const nsCString &nvInput, |
michael@0 | 159 | const nsACString &method, const nsACString &path, |
michael@0 | 160 | const nsACString &host, const nsACString &scheme, |
michael@0 | 161 | nsACString &output); |
michael@0 | 162 | |
michael@0 | 163 | int64_t GetParsedContentLength() { return mParsedContentLength; } // -1 on not found |
michael@0 | 164 | |
michael@0 | 165 | void SetMaxBufferSize(uint32_t maxBufferSize); |
michael@0 | 166 | nsresult SetMaxBufferSizeInternal(uint32_t maxBufferSize); |
michael@0 | 167 | |
michael@0 | 168 | protected: |
michael@0 | 169 | virtual void ClearHeaderTable() MOZ_OVERRIDE; |
michael@0 | 170 | virtual void UpdateReferenceSet(int32_t delta) MOZ_OVERRIDE; |
michael@0 | 171 | virtual void IncrementReferenceSetIndices() MOZ_OVERRIDE; |
michael@0 | 172 | virtual void MakeRoom(uint32_t amount) MOZ_OVERRIDE; |
michael@0 | 173 | |
michael@0 | 174 | private: |
michael@0 | 175 | enum outputCode { |
michael@0 | 176 | kPlainLiteral, |
michael@0 | 177 | kIndexedLiteral, |
michael@0 | 178 | kToggleOff, |
michael@0 | 179 | kToggleOn, |
michael@0 | 180 | kNop |
michael@0 | 181 | }; |
michael@0 | 182 | |
michael@0 | 183 | void DoOutput(Http2Compressor::outputCode code, |
michael@0 | 184 | const class nvPair *pair, uint32_t index); |
michael@0 | 185 | void EncodeInteger(uint32_t prefixLen, uint32_t val); |
michael@0 | 186 | void ProcessHeader(const nvPair inputPair); |
michael@0 | 187 | void HuffmanAppend(const nsCString &value); |
michael@0 | 188 | |
michael@0 | 189 | int64_t mParsedContentLength; |
michael@0 | 190 | uint32_t mMaxBufferSetting; |
michael@0 | 191 | |
michael@0 | 192 | nsAutoTArray<uint32_t, 64> mImpliedReferenceSet; |
michael@0 | 193 | }; |
michael@0 | 194 | |
michael@0 | 195 | } // namespace mozilla::net |
michael@0 | 196 | } // namespace mozilla |
michael@0 | 197 | |
michael@0 | 198 | #endif // mozilla_net_Http2Compression_Internal_h |