netwerk/protocol/http/Http2Compression.h

Thu, 15 Jan 2015 21:03:48 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 21:03:48 +0100
branch
TOR_BUG_9701
changeset 11
deefc01c0e14
permissions
-rw-r--r--

Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)

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

mercurial