Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / net / spdy / hpack_entry.cc
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.
4
5 #include "net/spdy/hpack_entry.h"
6
7 #include "base/logging.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "net/spdy/hpack_string_util.h"
10
11 namespace net {
12
13 using base::StringPiece;
14
15 const size_t HpackEntry::kSizeOverhead = 32;
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,
30                        StringPiece value,
31                        bool is_static,
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),
37       state_(0),
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));
42 }
43
44 HpackEntry::HpackEntry(StringPiece name, StringPiece value)
45     : name_(name.data(), name.size()),
46       value_(value.data(), value.size()),
47       is_static_(false),
48       state_(0),
49       insertion_index_(0),
50       total_insertions_or_size_(NULL) {
51 }
52
53 HpackEntry::HpackEntry()
54     : is_static_(false),
55       state_(0),
56       insertion_index_(0),
57       total_insertions_or_size_(NULL) {
58 }
59
60 HpackEntry::~HpackEntry() {}
61
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
74 size_t HpackEntry::Size(StringPiece name, StringPiece value) {
75   return name.size() + value.size() + kSizeOverhead;
76 }
77 size_t HpackEntry::Size() const {
78   return Size(name(), value());
79 }
80
81 std::string HpackEntry::GetDebugString() const {
82   return "{ name: \"" + name_ +
83       "\", value: \"" + value_ +
84       "\", " + (IsStatic() ? "static" : "dynamic") +
85       ", state: " + base::IntToString(state_) + " }";
86 }
87
88 }  // namespace net