| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/spdy/hpack_entry.h" | 5 #include "net/spdy/hpack_entry.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/strings/string_number_conversions.h" | 8 #include "base/strings/string_number_conversions.h" |
| 9 #include "net/spdy/hpack_string_util.h" | 9 #include "net/spdy/hpack_string_util.h" |
| 10 | 10 |
| 11 namespace net { | 11 namespace net { |
| 12 | 12 |
| 13 using base::StringPiece; | 13 using base::StringPiece; |
| 14 | 14 |
| 15 const size_t HpackEntry::kSizeOverhead = 32; | 15 const size_t HpackEntry::kSizeOverhead = 32; |
| 16 | 16 |
| 17 bool HpackEntry::Comparator::operator() ( | |
| 18 const HpackEntry* lhs, const HpackEntry* rhs) const { | |
| 19 int result = lhs->name().compare(rhs->name()); | |
| 20 if (result != 0) | |
| 21 return result < 0; | |
| 22 result = lhs->value().compare(rhs->value()); | |
| 23 if (result != 0) | |
| 24 return result < 0; | |
| 25 DCHECK(lhs == rhs || lhs->Index() != rhs->Index()); | |
| 26 return lhs->Index() < rhs->Index(); | |
| 27 } | |
| 28 | |
| 29 HpackEntry::HpackEntry(StringPiece name, | 17 HpackEntry::HpackEntry(StringPiece name, |
| 30 StringPiece value, | 18 StringPiece value, |
| 31 bool is_static, | 19 bool is_static, |
| 32 size_t insertion_index, | 20 size_t insertion_index) |
| 33 const size_t* total_table_insertions_or_current_size) | |
| 34 : name_(name.data(), name.size()), | 21 : name_(name.data(), name.size()), |
| 35 value_(value.data(), value.size()), | 22 value_(value.data(), value.size()), |
| 36 is_static_(is_static), | 23 insertion_index_(insertion_index), |
| 37 state_(0), | 24 state_(0), |
| 38 insertion_index_(insertion_index), | 25 type_(is_static ? STATIC : DYNAMIC) { |
| 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)); | |
| 42 } | 26 } |
| 43 | 27 |
| 44 HpackEntry::HpackEntry(StringPiece name, StringPiece value) | 28 HpackEntry::HpackEntry(StringPiece name, StringPiece value) |
| 45 : name_(name.data(), name.size()), | 29 : name_(name.data(), name.size()), |
| 46 value_(value.data(), value.size()), | 30 value_(value.data(), value.size()), |
| 47 is_static_(false), | 31 insertion_index_(0), |
| 48 state_(0), | 32 state_(0), |
| 49 insertion_index_(0), | 33 type_(LOOKUP) { |
| 50 total_insertions_or_size_(NULL) { | |
| 51 } | 34 } |
| 52 | 35 |
| 53 HpackEntry::HpackEntry() | 36 HpackEntry::HpackEntry() |
| 54 : is_static_(false), | 37 : insertion_index_(0), |
| 55 state_(0), | 38 state_(0), |
| 56 insertion_index_(0), | 39 type_(LOOKUP) { |
| 57 total_insertions_or_size_(NULL) { | |
| 58 } | 40 } |
| 59 | 41 |
| 60 HpackEntry::~HpackEntry() {} | 42 HpackEntry::~HpackEntry() {} |
| 61 | 43 |
| 62 size_t HpackEntry::Index() const { | |
| 63 if (total_insertions_or_size_ == NULL) { | |
| 64 // This is a lookup instance. | |
| 65 return 0; | |
| 66 } else if (IsStatic()) { | |
| 67 return 1 + insertion_index_ + *total_insertions_or_size_; | |
| 68 } else { | |
| 69 return *total_insertions_or_size_ - insertion_index_; | |
| 70 } | |
| 71 } | |
| 72 | |
| 73 // static | 44 // static |
| 74 size_t HpackEntry::Size(StringPiece name, StringPiece value) { | 45 size_t HpackEntry::Size(StringPiece name, StringPiece value) { |
| 75 return name.size() + value.size() + kSizeOverhead; | 46 return name.size() + value.size() + kSizeOverhead; |
| 76 } | 47 } |
| 77 size_t HpackEntry::Size() const { | 48 size_t HpackEntry::Size() const { |
| 78 return Size(name(), value()); | 49 return Size(name(), value()); |
| 79 } | 50 } |
| 80 | 51 |
| 81 std::string HpackEntry::GetDebugString() const { | 52 std::string HpackEntry::GetDebugString() const { |
| 82 return "{ name: \"" + name_ + | 53 return "{ name: \"" + name_ + |
| 83 "\", value: \"" + value_ + | 54 "\", value: \"" + value_ + |
| 84 "\", " + (IsStatic() ? "static" : "dynamic") + | 55 "\", " + (IsStatic() ? "static" : "dynamic") + |
| 85 ", state: " + base::IntToString(state_) + " }"; | 56 ", state: " + base::IntToString(state_) + " }"; |
| 86 } | 57 } |
| 87 | 58 |
| 88 } // namespace net | 59 } // namespace net |
| OLD | NEW |