1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/spdy/hpack_entry.h"
7 #include "base/logging.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "net/spdy/hpack_string_util.h"
13 using base::StringPiece;
15 const size_t HpackEntry::kSizeOverhead = 32;
17 bool HpackEntry::Comparator::operator() (
18 const HpackEntry* lhs, const HpackEntry* rhs) const {
19 int result = lhs->name().compare(rhs->name());
22 result = lhs->value().compare(rhs->value());
25 DCHECK(lhs == rhs || lhs->Index() != rhs->Index());
26 return lhs->Index() < rhs->Index();
29 HpackEntry::HpackEntry(StringPiece name,
32 size_t insertion_index,
33 const size_t* total_table_insertions_or_current_size)
34 : name_(name.data(), name.size()),
35 value_(value.data(), value.size()),
36 is_static_(is_static),
38 insertion_index_(insertion_index),
39 total_insertions_or_size_(total_table_insertions_or_current_size) {
40 CHECK_NE(total_table_insertions_or_current_size,
41 static_cast<const size_t*>(NULL));
44 HpackEntry::HpackEntry(StringPiece name, StringPiece value)
45 : name_(name.data(), name.size()),
46 value_(value.data(), value.size()),
50 total_insertions_or_size_(NULL) {
53 HpackEntry::HpackEntry()
57 total_insertions_or_size_(NULL) {
60 HpackEntry::~HpackEntry() {}
62 size_t HpackEntry::Index() const {
63 if (total_insertions_or_size_ == NULL) {
64 // This is a lookup instance.
66 } else if (IsStatic()) {
67 return 1 + insertion_index_ + *total_insertions_or_size_;
69 return *total_insertions_or_size_ - insertion_index_;
74 size_t HpackEntry::Size(StringPiece name, StringPiece value) {
75 return name.size() + value.size() + kSizeOverhead;
77 size_t HpackEntry::Size() const {
78 return Size(name(), value());
81 std::string HpackEntry::GetDebugString() const {
82 return "{ name: \"" + name_ +
83 "\", value: \"" + value_ +
84 "\", " + (IsStatic() ? "static" : "dynamic") +
85 ", state: " + base::IntToString(state_) + " }";