DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Implementation

Mercurial (c68fe15a81fc)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef mozilla_net_Http2Compression_Internal_h
#define mozilla_net_Http2Compression_Internal_h

// HPACK - RFC 7541
// HPACK - RFC 7541
// https://www.rfc-editor.org/rfc/rfc7541.txt

#include "mozilla/Attributes.h"
#include "nsDeque.h"
#include "nsString.h"
#include "mozilla/Telemetry.h"

namespace mozilla {
namespace net {
namespace net {

struct HuffmanIncomingTable;

void Http2CompressionCleanup();


class nvPair {
 public:
  nvPair(const nsACString& name, const nsACString& value)
      : mName(name), mValue(value) {}


  uint32_t Size() const { return mName.Length() + mValue.Length() + 32; }
  size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const;
  size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const;

  nsCString mName;
  nsCString mValue;
  nsCString mValue;
};

class nvFIFO {
 public:
  nvFIFO();
  ~nvFIFO();
  void AddElement(const nsCString& name, const nsCString& value);
  void AddElement(const nsCString& name);
  void RemoveElement();
  void RemoveElement();
  uint32_t ByteCount() const;
  uint32_t Length() const;
  uint32_t VariableLength() const;
  size_t StaticLength() const;
  void Clear();
  void Clear();
  const nvPair* operator[](size_t index) const;

 private:
  uint32_t mByteCount;
  nsDeque mTable;
  nsDeque mTable;
};

class HpackDynamicTableReporter;

class Http2BaseCompressor {
class Http2BaseCompressor {
 public:
  Http2BaseCompressor();
  virtual ~Http2BaseCompressor();
  size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
  nsresult SetInitialMaxBufferSize(uint32_t maxBufferSize);
  nsresult SetInitialMaxBufferSize(uint32_t maxBufferSize);
  void SetDumpTables(bool dumpTables);

 protected:
  const static uint32_t kDefaultMaxBuffer = 4096;


  virtual void ClearHeaderTable();
  virtual void MakeRoom(uint32_t amount, const char* direction);
  virtual void DumpState(const char*);
  virtual void SetMaxBufferSizeInternal(uint32_t maxBufferSize);


  nsACString* mOutput;
  nvFIFO mHeaderTable;

  uint32_t mMaxBuffer;
  uint32_t mMaxBufferSetting;
  uint32_t mMaxBufferSetting;
  bool mSetInitialMaxBufferSizeAllowed;

  uint32_t mPeakSize;
  uint32_t mPeakCount;
  MOZ_INIT_OUTSIDE_CTOR
  MOZ_INIT_OUTSIDE_CTOR
  Telemetry::HistogramID mPeakSizeID;
  MOZ_INIT_OUTSIDE_CTOR
  Telemetry::HistogramID mPeakCountID;

  bool mDumpTables;
  bool mDumpTables;

 private:
  RefPtr<HpackDynamicTableReporter> mDynamicReporter;
};


class Http2Compressor;

class Http2Decompressor final : public Http2BaseCompressor {
 public:
  Http2Decompressor()
  Http2Decompressor()
      : mOffset(0),
        mData(nullptr),
        mDataLen(0),
        mDataLen(0),
        mSeenNonColonHeader(false),
        mSeenNonColonHeader(false),
        mIsPush(false) {
    mPeakSizeID = Telemetry::HPACK_PEAK_SIZE_DECOMPRESSOR;
    mPeakCountID = Telemetry::HPACK_PEAK_COUNT_DECOMPRESSOR;
  };
  virtual ~Http2Decompressor() = default;

  // NS_OK: Produces the working set of HTTP/1 formatted headers
  // NS_OK: Produces the working set of HTTP/1 formatted headers
  [[nodiscard]] nsresult DecodeHeaderBlock(const uint8_t* data,
                                           uint32_t datalen, nsACString& output,
                                           bool isPush);

  void GetStatus(nsACString& hdr) { hdr = mHeaderStatus; }
  void GetHost(nsACString& hdr) { hdr = mHeaderHost; }
  void GetScheme(nsACString& hdr) { hdr = mHeaderScheme; }
  void GetScheme(nsACString& hdr) { hdr = mHeaderScheme; }
  void GetPath(nsACString& hdr) { hdr = mHeaderPath; }
  void GetMethod(nsACString& hdr) { hdr = mHeaderMethod; }

 private:
  [[nodiscard]] nsresult DoIndexed();
  [[nodiscard]] nsresult DoIndexed();
  [[nodiscard]] nsresult DoLiteralWithoutIndex();
  [[nodiscard]] nsresult DoLiteralWithIncremental();
  [[nodiscard]] nsresult DoLiteralInternal(nsACString&, nsACString&, uint32_t);
  [[nodiscard]] nsresult DoLiteralNeverIndexed();
  [[nodiscard]] nsresult DoContextUpdate();
  [[nodiscard]] nsresult DoContextUpdate();

  [[nodiscard]] nsresult DecodeInteger(uint32_t prefixLen, uint32_t& result);
  [[nodiscard]] nsresult OutputHeader(uint32_t index);
  [[nodiscard]] nsresult OutputHeader(const nsACString& name,
                                      const nsACString& value);
                                      const nsACString& value);

  [[nodiscard]] nsresult CopyHeaderString(uint32_t index, nsACString& name);
  [[nodiscard]] nsresult CopyStringFromInput(uint32_t index, nsACString& val);
  uint8_t ExtractByte(uint8_t bitsLeft, uint32_t& bytesConsumed);
  [[nodiscard]] nsresult CopyHuffmanStringFromInput(uint32_t index,
  [[nodiscard]] nsresult CopyHuffmanStringFromInput(uint32_t index,
                                                    nsACString& val);
  [[nodiscard]] nsresult DecodeHuffmanCharacter(
      const HuffmanIncomingTable* table, uint8_t& c, uint32_t& bytesConsumed,
      uint8_t& bitsLeft);
  [[nodiscard]] nsresult DecodeFinalHuffmanCharacter(
  [[nodiscard]] nsresult DecodeFinalHuffmanCharacter(
      const HuffmanIncomingTable* table, uint8_t& c, uint8_t& bitsLeft);

  nsCString mHeaderStatus;
  nsCString mHeaderHost;
  nsCString mHeaderScheme;
  nsCString mHeaderScheme;
  nsCString mHeaderPath;
  nsCString mHeaderMethod;

  // state variables when DecodeBlock() is on the stack
  uint32_t mOffset;
  uint32_t mOffset;
  const uint8_t* mData;
  uint32_t mDataLen;
  bool mSeenNonColonHeader;
  bool mIsPush;
};
};

class Http2Compressor final : public Http2BaseCompressor {
 public:
  Http2Compressor()
      : mParsedContentLength(-1),
      : mParsedContentLength(-1),
        mBufferSizeChangeWaiting(false),
        mLowestBufferSizeWaiting(0) {
    mPeakSizeID = Telemetry::HPACK_PEAK_SIZE_COMPRESSOR;
    mPeakCountID = Telemetry::HPACK_PEAK_COUNT_COMPRESSOR;
    mPeakCountID = Telemetry::HPACK_PEAK_COUNT_COMPRESSOR;
  };
  virtual ~Http2Compressor() = default;

  // HTTP/1 formatted header block as input - HTTP/2 formatted
  // header block as output
  // header block as output
  [[nodiscard]] nsresult EncodeHeaderBlock(
      const nsCString& nvInput, const nsACString& method,
      const nsACString& path, const nsACString& host, const nsACString& scheme,
      const nsACString& protocol, bool simpleConnectForm, nsACString& output);


  int64_t GetParsedContentLength() {
    return mParsedContentLength;
  }  // -1 on not found

  void SetMaxBufferSize(uint32_t maxBufferSize);
  void SetMaxBufferSize(uint32_t maxBufferSize);

 private:
  enum outputCode {
    kNeverIndexedLiteral,
    kPlainLiteral,
    kPlainLiteral,
    kIndexedLiteral,
    kIndex
  };

  void DoOutput(Http2Compressor::outputCode code, const class nvPair* pair,
  void DoOutput(Http2Compressor::outputCode code, const class nvPair* pair,
                uint32_t index);
  void EncodeInteger(uint32_t prefixLen, uint32_t val);
  void ProcessHeader(const nvPair inputPair, bool noLocalIndex,
                     bool neverIndex);
  void HuffmanAppend(const nsCString& value);
  void EncodeTableSizeChange(uint32_t newMaxSize);


  int64_t mParsedContentLength;
  bool mBufferSizeChangeWaiting;
  uint32_t mLowestBufferSizeWaiting;
};


}  // namespace net
}  // namespace mozilla

#endif  // mozilla_net_Http2Compression_Internal_h