michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef mozilla_net_Http2Compression_Internal_h michael@0: #define mozilla_net_Http2Compression_Internal_h michael@0: michael@0: // HPACK michael@0: // tools.ietf.org/html/draft-ietf-httpbis-header-compression-04 michael@0: michael@0: #include "mozilla/Attributes.h" michael@0: #include "nsDeque.h" michael@0: #include "nsString.h" michael@0: michael@0: namespace mozilla { michael@0: namespace net { michael@0: michael@0: struct HuffmanIncomingTable; michael@0: michael@0: void Http2CompressionCleanup(); michael@0: michael@0: class nvPair michael@0: { michael@0: public: michael@0: nvPair(const nsACString &name, const nsACString &value) michael@0: : mName(name) michael@0: , mValue(value) michael@0: { } michael@0: michael@0: uint32_t Size() const { return mName.Length() + mValue.Length() + 32; } michael@0: michael@0: nsCString mName; michael@0: nsCString mValue; michael@0: }; michael@0: michael@0: class nvFIFO michael@0: { michael@0: public: michael@0: nvFIFO(); michael@0: ~nvFIFO(); michael@0: void AddElement(const nsCString &name, const nsCString &value); michael@0: void AddElement(const nsCString &name); michael@0: void RemoveElement(); michael@0: uint32_t ByteCount() const; michael@0: uint32_t Length() const; michael@0: uint32_t VariableLength() const; michael@0: void Clear(); michael@0: const nvPair *operator[] (int32_t index) const; michael@0: michael@0: private: michael@0: uint32_t mByteCount; michael@0: nsDeque mTable; michael@0: }; michael@0: michael@0: class Http2BaseCompressor michael@0: { michael@0: public: michael@0: Http2BaseCompressor(); michael@0: virtual ~Http2BaseCompressor() { }; michael@0: michael@0: protected: michael@0: // this will become a HTTP/2 SETTINGS value in a future draft michael@0: const static uint32_t kDefaultMaxBuffer = 4096; michael@0: michael@0: virtual void ClearHeaderTable(); michael@0: virtual void UpdateReferenceSet(int32_t delta); michael@0: virtual void IncrementReferenceSetIndices(); michael@0: virtual void MakeRoom(uint32_t amount) = 0; michael@0: michael@0: nsAutoTArray mReferenceSet; // list of indicies michael@0: michael@0: // the alternate set is used to track the emitted headers when michael@0: // processing input for a header set. The input to the compressor michael@0: // is a series of nvpairs, the input to the decompressor is the michael@0: // series of op codes that make up the header block. michael@0: // michael@0: // after processing the input the compressor compares the alternate michael@0: // set to the inherited reference set and generates indicies to michael@0: // toggle off any members of alternate - inherited. the alternate michael@0: // then becomes the inherited set for the next header set. michael@0: // michael@0: // after processing the input the decompressor comapres the alternate michael@0: // set to the inherited reference set and generates headers for michael@0: // anything implicit in reference - alternate. michael@0: nsAutoTArray mAlternateReferenceSet; // list of indicies michael@0: michael@0: nsACString *mOutput; michael@0: nvFIFO mHeaderTable; michael@0: michael@0: uint32_t mMaxBuffer; michael@0: }; michael@0: michael@0: class Http2Compressor; michael@0: michael@0: class Http2Decompressor MOZ_FINAL : public Http2BaseCompressor michael@0: { michael@0: public: michael@0: Http2Decompressor() { }; michael@0: virtual ~Http2Decompressor() { } ; michael@0: michael@0: // NS_OK: Produces the working set of HTTP/1 formatted headers michael@0: nsresult DecodeHeaderBlock(const uint8_t *data, uint32_t datalen, michael@0: nsACString &output); michael@0: michael@0: void GetStatus(nsACString &hdr) { hdr = mHeaderStatus; } michael@0: void GetHost(nsACString &hdr) { hdr = mHeaderHost; } michael@0: void GetScheme(nsACString &hdr) { hdr = mHeaderScheme; } michael@0: void GetPath(nsACString &hdr) { hdr = mHeaderPath; } michael@0: void GetMethod(nsACString &hdr) { hdr = mHeaderMethod; } michael@0: void SetCompressor(Http2Compressor *compressor) { mCompressor = compressor; } michael@0: michael@0: protected: michael@0: virtual void MakeRoom(uint32_t amount) MOZ_OVERRIDE; michael@0: michael@0: private: michael@0: nsresult DoIndexed(); michael@0: nsresult DoLiteralWithoutIndex(); michael@0: nsresult DoLiteralWithIncremental(); michael@0: nsresult DoLiteralInternal(nsACString &, nsACString &); michael@0: michael@0: nsresult DecodeInteger(uint32_t prefixLen, uint32_t &result); michael@0: nsresult OutputHeader(uint32_t index); michael@0: nsresult OutputHeader(const nsACString &name, const nsACString &value); michael@0: michael@0: nsresult CopyHeaderString(uint32_t index, nsACString &name); michael@0: nsresult CopyStringFromInput(uint32_t index, nsACString &val); michael@0: uint8_t ExtractByte(uint8_t bitsLeft, uint32_t &bytesConsumed); michael@0: nsresult CopyHuffmanStringFromInput(uint32_t index, nsACString &val); michael@0: nsresult DecodeHuffmanCharacter(HuffmanIncomingTable *table, uint8_t &c, michael@0: uint32_t &bytesConsumed, uint8_t &bitsLeft); michael@0: nsresult DecodeFinalHuffmanCharacter(HuffmanIncomingTable *table, uint8_t &c, michael@0: uint8_t &bitsLeft); michael@0: michael@0: Http2Compressor *mCompressor; michael@0: michael@0: nsCString mHeaderStatus; michael@0: nsCString mHeaderHost; michael@0: nsCString mHeaderScheme; michael@0: nsCString mHeaderPath; michael@0: nsCString mHeaderMethod; michael@0: michael@0: // state variables when DecodeBlock() is on the stack michael@0: uint32_t mOffset; michael@0: const uint8_t *mData; michael@0: uint32_t mDataLen; michael@0: }; michael@0: michael@0: michael@0: class Http2Compressor MOZ_FINAL : public Http2BaseCompressor michael@0: { michael@0: public: michael@0: Http2Compressor() : mParsedContentLength(-1) { }; michael@0: virtual ~Http2Compressor() { } michael@0: michael@0: // HTTP/1 formatted header block as input - HTTP/2 formatted michael@0: // header block as output michael@0: nsresult EncodeHeaderBlock(const nsCString &nvInput, michael@0: const nsACString &method, const nsACString &path, michael@0: const nsACString &host, const nsACString &scheme, michael@0: nsACString &output); michael@0: michael@0: int64_t GetParsedContentLength() { return mParsedContentLength; } // -1 on not found michael@0: michael@0: void SetMaxBufferSize(uint32_t maxBufferSize); michael@0: nsresult SetMaxBufferSizeInternal(uint32_t maxBufferSize); michael@0: michael@0: protected: michael@0: virtual void ClearHeaderTable() MOZ_OVERRIDE; michael@0: virtual void UpdateReferenceSet(int32_t delta) MOZ_OVERRIDE; michael@0: virtual void IncrementReferenceSetIndices() MOZ_OVERRIDE; michael@0: virtual void MakeRoom(uint32_t amount) MOZ_OVERRIDE; michael@0: michael@0: private: michael@0: enum outputCode { michael@0: kPlainLiteral, michael@0: kIndexedLiteral, michael@0: kToggleOff, michael@0: kToggleOn, michael@0: kNop michael@0: }; michael@0: michael@0: void DoOutput(Http2Compressor::outputCode code, michael@0: const class nvPair *pair, uint32_t index); michael@0: void EncodeInteger(uint32_t prefixLen, uint32_t val); michael@0: void ProcessHeader(const nvPair inputPair); michael@0: void HuffmanAppend(const nsCString &value); michael@0: michael@0: int64_t mParsedContentLength; michael@0: uint32_t mMaxBufferSetting; michael@0: michael@0: nsAutoTArray mImpliedReferenceSet; michael@0: }; michael@0: michael@0: } // namespace mozilla::net michael@0: } // namespace mozilla michael@0: michael@0: #endif // mozilla_net_Http2Compression_Internal_h