2013-10-10 04:21:49 +04:00
|
|
|
/* -*- 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/. */
|
|
|
|
|
|
|
|
#ifndef mozilla_net_Http2Compression_Internal_h
|
|
|
|
#define mozilla_net_Http2Compression_Internal_h
|
|
|
|
|
|
|
|
// HPACK
|
|
|
|
// tools.ietf.org/html/draft-ietf-httpbis-header-compression-04
|
|
|
|
|
|
|
|
#include "mozilla/Attributes.h"
|
|
|
|
#include "nsDeque.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
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; }
|
|
|
|
|
|
|
|
nsCString mName;
|
|
|
|
nsCString mValue;
|
|
|
|
};
|
|
|
|
|
|
|
|
class nvFIFO
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
nvFIFO();
|
|
|
|
~nvFIFO();
|
|
|
|
void AddElement(const nsCString &name, const nsCString &value);
|
|
|
|
void AddElement(const nsCString &name);
|
|
|
|
void RemoveElement();
|
|
|
|
uint32_t ByteCount() const;
|
|
|
|
uint32_t Length() const;
|
|
|
|
uint32_t VariableLength() const;
|
2014-08-05 19:41:09 +04:00
|
|
|
uint32_t StaticLength() const;
|
2013-10-10 04:21:49 +04:00
|
|
|
void Clear();
|
|
|
|
const nvPair *operator[] (int32_t index) const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
uint32_t mByteCount;
|
|
|
|
nsDeque mTable;
|
|
|
|
};
|
|
|
|
|
|
|
|
class Http2BaseCompressor
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Http2BaseCompressor();
|
|
|
|
virtual ~Http2BaseCompressor() { };
|
|
|
|
|
|
|
|
protected:
|
|
|
|
// this will become a HTTP/2 SETTINGS value in a future draft
|
|
|
|
const static uint32_t kDefaultMaxBuffer = 4096;
|
|
|
|
|
|
|
|
virtual void ClearHeaderTable();
|
2014-08-05 19:41:09 +04:00
|
|
|
virtual void MakeRoom(uint32_t amount, const char *direction);
|
2014-05-31 03:16:23 +04:00
|
|
|
virtual void DumpState();
|
2013-10-10 04:21:49 +04:00
|
|
|
|
|
|
|
nsACString *mOutput;
|
|
|
|
nvFIFO mHeaderTable;
|
|
|
|
|
|
|
|
uint32_t mMaxBuffer;
|
|
|
|
};
|
|
|
|
|
2014-03-26 21:58:09 +04:00
|
|
|
class Http2Compressor;
|
|
|
|
|
2013-10-10 04:21:49 +04:00
|
|
|
class Http2Decompressor MOZ_FINAL : public Http2BaseCompressor
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Http2Decompressor() { };
|
|
|
|
virtual ~Http2Decompressor() { } ;
|
|
|
|
|
|
|
|
// NS_OK: Produces the working set of HTTP/1 formatted headers
|
|
|
|
nsresult DecodeHeaderBlock(const uint8_t *data, uint32_t datalen,
|
2014-08-19 01:59:32 +04:00
|
|
|
nsACString &output, bool isPush);
|
2013-10-10 04:21:49 +04:00
|
|
|
|
|
|
|
void GetStatus(nsACString &hdr) { hdr = mHeaderStatus; }
|
|
|
|
void GetHost(nsACString &hdr) { hdr = mHeaderHost; }
|
|
|
|
void GetScheme(nsACString &hdr) { hdr = mHeaderScheme; }
|
|
|
|
void GetPath(nsACString &hdr) { hdr = mHeaderPath; }
|
|
|
|
void GetMethod(nsACString &hdr) { hdr = mHeaderMethod; }
|
2014-03-26 21:58:09 +04:00
|
|
|
void SetCompressor(Http2Compressor *compressor) { mCompressor = compressor; }
|
2013-10-10 04:21:49 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
nsresult DoIndexed();
|
|
|
|
nsresult DoLiteralWithoutIndex();
|
|
|
|
nsresult DoLiteralWithIncremental();
|
2014-04-30 05:45:59 +04:00
|
|
|
nsresult DoLiteralInternal(nsACString &, nsACString &, uint32_t);
|
|
|
|
nsresult DoLiteralNeverIndexed();
|
|
|
|
nsresult DoContextUpdate();
|
2013-10-10 04:21:49 +04:00
|
|
|
|
|
|
|
nsresult DecodeInteger(uint32_t prefixLen, uint32_t &result);
|
|
|
|
nsresult OutputHeader(uint32_t index);
|
|
|
|
nsresult OutputHeader(const nsACString &name, const nsACString &value);
|
|
|
|
|
|
|
|
nsresult CopyHeaderString(uint32_t index, nsACString &name);
|
|
|
|
nsresult CopyStringFromInput(uint32_t index, nsACString &val);
|
2014-03-26 21:58:09 +04:00
|
|
|
uint8_t ExtractByte(uint8_t bitsLeft, uint32_t &bytesConsumed);
|
2013-10-10 04:21:49 +04:00
|
|
|
nsresult CopyHuffmanStringFromInput(uint32_t index, nsACString &val);
|
|
|
|
nsresult DecodeHuffmanCharacter(HuffmanIncomingTable *table, uint8_t &c,
|
|
|
|
uint32_t &bytesConsumed, uint8_t &bitsLeft);
|
|
|
|
nsresult DecodeFinalHuffmanCharacter(HuffmanIncomingTable *table, uint8_t &c,
|
|
|
|
uint8_t &bitsLeft);
|
|
|
|
|
2014-03-26 21:58:09 +04:00
|
|
|
Http2Compressor *mCompressor;
|
|
|
|
|
2013-10-10 04:21:49 +04:00
|
|
|
nsCString mHeaderStatus;
|
|
|
|
nsCString mHeaderHost;
|
|
|
|
nsCString mHeaderScheme;
|
|
|
|
nsCString mHeaderPath;
|
|
|
|
nsCString mHeaderMethod;
|
|
|
|
|
|
|
|
// state variables when DecodeBlock() is on the stack
|
|
|
|
uint32_t mOffset;
|
|
|
|
const uint8_t *mData;
|
|
|
|
uint32_t mDataLen;
|
2014-08-05 19:41:09 +04:00
|
|
|
bool mSeenNonColonHeader;
|
2014-08-19 01:59:32 +04:00
|
|
|
bool mIsPush;
|
2013-10-10 04:21:49 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class Http2Compressor MOZ_FINAL : public Http2BaseCompressor
|
|
|
|
{
|
|
|
|
public:
|
2014-04-30 05:46:07 +04:00
|
|
|
Http2Compressor() : mParsedContentLength(-1),
|
|
|
|
mMaxBufferSetting(kDefaultMaxBuffer),
|
|
|
|
mBufferSizeChangeWaiting(false),
|
|
|
|
mLowestBufferSizeWaiting(0)
|
|
|
|
{ };
|
2013-10-10 04:21:49 +04:00
|
|
|
virtual ~Http2Compressor() { }
|
|
|
|
|
|
|
|
// HTTP/1 formatted header block as input - HTTP/2 formatted
|
|
|
|
// header block as output
|
|
|
|
nsresult EncodeHeaderBlock(const nsCString &nvInput,
|
|
|
|
const nsACString &method, const nsACString &path,
|
|
|
|
const nsACString &host, const nsACString &scheme,
|
2014-05-16 19:46:13 +04:00
|
|
|
bool connectForm, nsACString &output);
|
2013-10-10 04:21:49 +04:00
|
|
|
|
|
|
|
int64_t GetParsedContentLength() { return mParsedContentLength; } // -1 on not found
|
|
|
|
|
|
|
|
void SetMaxBufferSize(uint32_t maxBufferSize);
|
2014-03-26 21:58:09 +04:00
|
|
|
nsresult SetMaxBufferSizeInternal(uint32_t maxBufferSize);
|
2013-10-10 04:21:49 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
enum outputCode {
|
2014-04-30 05:45:59 +04:00
|
|
|
kNeverIndexedLiteral,
|
2013-10-10 04:21:49 +04:00
|
|
|
kPlainLiteral,
|
|
|
|
kIndexedLiteral,
|
2014-08-05 19:41:09 +04:00
|
|
|
kIndex
|
2013-10-10 04:21:49 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
void DoOutput(Http2Compressor::outputCode code,
|
|
|
|
const class nvPair *pair, uint32_t index);
|
|
|
|
void EncodeInteger(uint32_t prefixLen, uint32_t val);
|
2014-06-14 02:02:05 +04:00
|
|
|
void ProcessHeader(const nvPair inputPair, bool noLocalIndex,
|
|
|
|
bool neverIndex);
|
2013-10-10 04:21:49 +04:00
|
|
|
void HuffmanAppend(const nsCString &value);
|
2014-04-30 05:46:07 +04:00
|
|
|
void EncodeTableSizeChange(uint32_t newMaxSize);
|
2013-10-10 04:21:49 +04:00
|
|
|
|
|
|
|
int64_t mParsedContentLength;
|
2014-03-26 21:58:09 +04:00
|
|
|
uint32_t mMaxBufferSetting;
|
2014-04-30 05:46:07 +04:00
|
|
|
bool mBufferSizeChangeWaiting;
|
|
|
|
uint32_t mLowestBufferSizeWaiting;
|
2013-10-10 04:21:49 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mozilla::net
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // mozilla_net_Http2Compression_Internal_h
|