This source file includes following definitions.
- GetMutableEntryCount
- GetEntryCount
- GetNameAt
- GetValueAt
- IsReferencedAt
- GetTouchCountAt
- SetReferencedAt
- AddTouchesAt
- ClearTouchesAt
- ApplyHeaderTableSizeSetting
- ProcessContextUpdateNewMaximumSize
- ProcessContextUpdateEmptyReferenceSet
- ProcessIndexedHeader
- ProcessLiteralHeaderWithIncrementalIndexing
#include "net/spdy/hpack_encoding_context.h"
#include <cstddef>
#include "base/logging.h"
#include "base/macros.h"
#include "net/spdy/hpack_constants.h"
#include "net/spdy/hpack_entry.h"
namespace net {
using base::StringPiece;
namespace {
struct StaticEntry {
const char* const name;
const size_t name_len;
const char* const value;
const size_t value_len;
};
#define STATIC_ENTRY(name, value) \
{ name, arraysize(name) - 1, value, arraysize(value) - 1 }
const StaticEntry kStaticTable[] = {
STATIC_ENTRY(":authority" , ""),
STATIC_ENTRY(":method" , "GET"),
STATIC_ENTRY(":method" , "POST"),
STATIC_ENTRY(":path" , "/"),
STATIC_ENTRY(":path" , "/index.html"),
STATIC_ENTRY(":scheme" , "http"),
STATIC_ENTRY(":scheme" , "https"),
STATIC_ENTRY(":status" , "200"),
STATIC_ENTRY(":status" , "500"),
STATIC_ENTRY(":status" , "404"),
STATIC_ENTRY(":status" , "403"),
STATIC_ENTRY(":status" , "400"),
STATIC_ENTRY(":status" , "401"),
STATIC_ENTRY("accept-charset" , ""),
STATIC_ENTRY("accept-encoding" , ""),
STATIC_ENTRY("accept-language" , ""),
STATIC_ENTRY("accept-ranges" , ""),
STATIC_ENTRY("accept" , ""),
STATIC_ENTRY("access-control-allow-origin" , ""),
STATIC_ENTRY("age" , ""),
STATIC_ENTRY("allow" , ""),
STATIC_ENTRY("authorization" , ""),
STATIC_ENTRY("cache-control" , ""),
STATIC_ENTRY("content-disposition" , ""),
STATIC_ENTRY("content-encoding" , ""),
STATIC_ENTRY("content-language" , ""),
STATIC_ENTRY("content-length" , ""),
STATIC_ENTRY("content-location" , ""),
STATIC_ENTRY("content-range" , ""),
STATIC_ENTRY("content-type" , ""),
STATIC_ENTRY("cookie" , ""),
STATIC_ENTRY("date" , ""),
STATIC_ENTRY("etag" , ""),
STATIC_ENTRY("expect" , ""),
STATIC_ENTRY("expires" , ""),
STATIC_ENTRY("from" , ""),
STATIC_ENTRY("host" , ""),
STATIC_ENTRY("if-match" , ""),
STATIC_ENTRY("if-modified-since" , ""),
STATIC_ENTRY("if-none-match" , ""),
STATIC_ENTRY("if-range" , ""),
STATIC_ENTRY("if-unmodified-since" , ""),
STATIC_ENTRY("last-modified" , ""),
STATIC_ENTRY("link" , ""),
STATIC_ENTRY("location" , ""),
STATIC_ENTRY("max-forwards" , ""),
STATIC_ENTRY("proxy-authenticate" , ""),
STATIC_ENTRY("proxy-authorization" , ""),
STATIC_ENTRY("range" , ""),
STATIC_ENTRY("referer" , ""),
STATIC_ENTRY("refresh" , ""),
STATIC_ENTRY("retry-after" , ""),
STATIC_ENTRY("server" , ""),
STATIC_ENTRY("set-cookie" , ""),
STATIC_ENTRY("strict-transport-security" , ""),
STATIC_ENTRY("transfer-encoding" , ""),
STATIC_ENTRY("user-agent" , ""),
STATIC_ENTRY("vary" , ""),
STATIC_ENTRY("via" , ""),
STATIC_ENTRY("www-authenticate" , ""),
};
#undef STATIC_ENTRY
const size_t kStaticEntryCount = arraysize(kStaticTable);
}
const uint32 HpackEncodingContext::kUntouched = 0x7fffffff;
HpackEncodingContext::HpackEncodingContext()
: settings_header_table_size_(kDefaultHeaderTableSizeSetting) {
DCHECK_EQ(HpackEncodingContext::kUntouched, HpackEntry::kUntouched);
}
HpackEncodingContext::~HpackEncodingContext() {}
uint32 HpackEncodingContext::GetMutableEntryCount() const {
return header_table_.GetEntryCount();
}
uint32 HpackEncodingContext::GetEntryCount() const {
return GetMutableEntryCount() + kStaticEntryCount;
}
StringPiece HpackEncodingContext::GetNameAt(uint32 index) const {
CHECK_GE(index, 1u);
CHECK_LE(index, GetEntryCount());
if (index > header_table_.GetEntryCount()) {
const StaticEntry& entry =
kStaticTable[index - header_table_.GetEntryCount() - 1];
return StringPiece(entry.name, entry.name_len);
}
return header_table_.GetEntry(index).name();
}
StringPiece HpackEncodingContext::GetValueAt(uint32 index) const {
CHECK_GE(index, 1u);
CHECK_LE(index, GetEntryCount());
if (index > header_table_.GetEntryCount()) {
const StaticEntry& entry =
kStaticTable[index - header_table_.GetEntryCount() - 1];
return StringPiece(entry.value, entry.value_len);
}
return header_table_.GetEntry(index).value();
}
bool HpackEncodingContext::IsReferencedAt(uint32 index) const {
CHECK_GE(index, 1u);
CHECK_LE(index, GetEntryCount());
if (index > header_table_.GetEntryCount())
return false;
return header_table_.GetEntry(index).IsReferenced();
}
uint32 HpackEncodingContext::GetTouchCountAt(uint32 index) const {
CHECK_GE(index, 1u);
CHECK_LE(index, GetEntryCount());
if (index > header_table_.GetEntryCount())
return 0;
return header_table_.GetEntry(index).TouchCount();
}
void HpackEncodingContext::SetReferencedAt(uint32 index, bool referenced) {
header_table_.GetMutableEntry(index)->SetReferenced(referenced);
}
void HpackEncodingContext::AddTouchesAt(uint32 index, uint32 touch_count) {
header_table_.GetMutableEntry(index)->AddTouches(touch_count);
}
void HpackEncodingContext::ClearTouchesAt(uint32 index) {
header_table_.GetMutableEntry(index)->ClearTouches();
}
void HpackEncodingContext::ApplyHeaderTableSizeSetting(uint32 size) {
settings_header_table_size_ = size;
if (size < header_table_.max_size()) {
CHECK(ProcessContextUpdateNewMaximumSize(size));
}
}
bool HpackEncodingContext::ProcessContextUpdateNewMaximumSize(uint32 size) {
if (size > settings_header_table_size_) {
return false;
}
header_table_.SetMaxSize(size);
return true;
}
bool HpackEncodingContext::ProcessContextUpdateEmptyReferenceSet() {
for (size_t i = 1; i <= header_table_.GetEntryCount(); ++i) {
HpackEntry* entry = header_table_.GetMutableEntry(i);
if (entry->IsReferenced()) {
entry->SetReferenced(false);
}
}
return true;
}
bool HpackEncodingContext::ProcessIndexedHeader(uint32 index, uint32* new_index,
std::vector<uint32>* removed_referenced_indices) {
CHECK_GT(index, 0u);
CHECK_LT(index, GetEntryCount());
if (index <= header_table_.GetEntryCount()) {
*new_index = index;
removed_referenced_indices->clear();
HpackEntry* entry = header_table_.GetMutableEntry(index);
entry->SetReferenced(!entry->IsReferenced());
} else {
HpackEntry entry(GetNameAt(index), GetValueAt(index));
header_table_.TryAddEntry(entry, new_index, removed_referenced_indices);
if (*new_index >= 1) {
header_table_.GetMutableEntry(*new_index)->SetReferenced(true);
}
}
return true;
}
bool HpackEncodingContext::ProcessLiteralHeaderWithIncrementalIndexing(
StringPiece name,
StringPiece value,
uint32* index,
std::vector<uint32>* removed_referenced_indices) {
HpackEntry entry(name, value);
header_table_.TryAddEntry(entry, index, removed_referenced_indices);
if (*index >= 1) {
header_table_.GetMutableEntry(*index)->SetReferenced(true);
}
return true;
}
}