OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 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_encoding_context.h" |
| 6 |
| 7 #include <cstddef> |
| 8 |
| 9 #include "base/logging.h" |
| 10 #include "base/macros.h" |
| 11 #include "net/spdy/hpack_entry.h" |
| 12 |
| 13 namespace net { |
| 14 |
| 15 namespace { |
| 16 |
| 17 // An entry in the static table. Must be a POD in order to avoid |
| 18 // static initializers, i.e. no user-defined constructors or |
| 19 // destructors. |
| 20 struct StaticEntry { |
| 21 const char* const name; |
| 22 const size_t name_len; |
| 23 const char* const value; |
| 24 const size_t value_len; |
| 25 }; |
| 26 |
| 27 // The "constructor" for a StaticEntry that computes the lengths at |
| 28 // compile time. |
| 29 #define STATIC_ENTRY(name, value) \ |
| 30 { name, arraysize(name) - 1, value, arraysize(value) - 1 } |
| 31 |
| 32 const StaticEntry kStaticTable[] = { |
| 33 STATIC_ENTRY(":authority" , ""), // 0 |
| 34 STATIC_ENTRY(":method" , "GET"), // 1 |
| 35 STATIC_ENTRY(":method" , "POST"), // 2 |
| 36 STATIC_ENTRY(":path" , "/"), // 3 |
| 37 STATIC_ENTRY(":path" , "/index.html"), // 4 |
| 38 STATIC_ENTRY(":scheme" , "http"), // 5 |
| 39 STATIC_ENTRY(":scheme" , "https"), // 6 |
| 40 STATIC_ENTRY(":status" , "200"), // 7 |
| 41 STATIC_ENTRY(":status" , "500"), // 8 |
| 42 STATIC_ENTRY(":status" , "404"), // 9 |
| 43 STATIC_ENTRY(":status" , "403"), // 10 |
| 44 STATIC_ENTRY(":status" , "400"), // 11 |
| 45 STATIC_ENTRY(":status" , "401"), // 12 |
| 46 STATIC_ENTRY("accept-charset" , ""), // 13 |
| 47 STATIC_ENTRY("accept-encoding" , ""), // 14 |
| 48 STATIC_ENTRY("accept-language" , ""), // 15 |
| 49 STATIC_ENTRY("accept-ranges" , ""), // 16 |
| 50 STATIC_ENTRY("accept" , ""), // 17 |
| 51 STATIC_ENTRY("access-control-allow-origin" , ""), // 18 |
| 52 STATIC_ENTRY("age" , ""), // 19 |
| 53 STATIC_ENTRY("allow" , ""), // 20 |
| 54 STATIC_ENTRY("authorization" , ""), // 21 |
| 55 STATIC_ENTRY("cache-control" , ""), // 22 |
| 56 STATIC_ENTRY("content-disposition" , ""), // 23 |
| 57 STATIC_ENTRY("content-encoding" , ""), // 24 |
| 58 STATIC_ENTRY("content-language" , ""), // 25 |
| 59 STATIC_ENTRY("content-length" , ""), // 26 |
| 60 STATIC_ENTRY("content-location" , ""), // 27 |
| 61 STATIC_ENTRY("content-range" , ""), // 28 |
| 62 STATIC_ENTRY("content-type" , ""), // 29 |
| 63 STATIC_ENTRY("cookie" , ""), // 30 |
| 64 STATIC_ENTRY("date" , ""), // 31 |
| 65 STATIC_ENTRY("etag" , ""), // 32 |
| 66 STATIC_ENTRY("expect" , ""), // 33 |
| 67 STATIC_ENTRY("expires" , ""), // 34 |
| 68 STATIC_ENTRY("from" , ""), // 35 |
| 69 STATIC_ENTRY("if-match" , ""), // 36 |
| 70 STATIC_ENTRY("if-modified-since" , ""), // 37 |
| 71 STATIC_ENTRY("if-none-match" , ""), // 38 |
| 72 STATIC_ENTRY("if-range" , ""), // 39 |
| 73 STATIC_ENTRY("if-unmodified-since" , ""), // 40 |
| 74 STATIC_ENTRY("last-modified" , ""), // 41 |
| 75 STATIC_ENTRY("link" , ""), // 42 |
| 76 STATIC_ENTRY("location" , ""), // 43 |
| 77 STATIC_ENTRY("max-forwards" , ""), // 44 |
| 78 STATIC_ENTRY("proxy-authenticate" , ""), // 45 |
| 79 STATIC_ENTRY("proxy-authorization" , ""), // 46 |
| 80 STATIC_ENTRY("range" , ""), // 47 |
| 81 STATIC_ENTRY("referer" , ""), // 48 |
| 82 STATIC_ENTRY("refresh" , ""), // 49 |
| 83 STATIC_ENTRY("retry-after" , ""), // 50 |
| 84 STATIC_ENTRY("server" , ""), // 51 |
| 85 STATIC_ENTRY("set-cookie" , ""), // 52 |
| 86 STATIC_ENTRY("strict-transport-security" , ""), // 53 |
| 87 STATIC_ENTRY("transfer-encoding" , ""), // 54 |
| 88 STATIC_ENTRY("user-agent" , ""), // 55 |
| 89 STATIC_ENTRY("vary" , ""), // 56 |
| 90 STATIC_ENTRY("via" , ""), // 57 |
| 91 STATIC_ENTRY("www-authenticate" , ""), // 58 |
| 92 }; |
| 93 |
| 94 #undef STATIC_ENTRY |
| 95 |
| 96 const size_t kStaticEntryCount = arraysize(kStaticTable); |
| 97 |
| 98 } // namespace |
| 99 |
| 100 const uint32 HpackEncodingContext::kUntouched = HpackEntry::kUntouched; |
| 101 |
| 102 HpackEncodingContext::HpackEncodingContext() {} |
| 103 |
| 104 HpackEncodingContext::~HpackEncodingContext() {} |
| 105 |
| 106 uint32 HpackEncodingContext::GetEntryCount() const { |
| 107 return header_table_.GetEntryCount() + kStaticEntryCount; |
| 108 } |
| 109 |
| 110 base::StringPiece HpackEncodingContext::GetNameAt(uint32 index) const { |
| 111 CHECK_LT(index, GetEntryCount()); |
| 112 if (index >= header_table_.GetEntryCount()) { |
| 113 const StaticEntry& entry = |
| 114 kStaticTable[index - header_table_.GetEntryCount()]; |
| 115 return base::StringPiece(entry.name, entry.name_len); |
| 116 } |
| 117 return header_table_.GetEntry(index).name(); |
| 118 } |
| 119 |
| 120 base::StringPiece HpackEncodingContext::GetValueAt(uint32 index) const { |
| 121 CHECK_LT(index, GetEntryCount()); |
| 122 if (index >= header_table_.GetEntryCount()) { |
| 123 const StaticEntry& entry = |
| 124 kStaticTable[index - header_table_.GetEntryCount()]; |
| 125 return base::StringPiece(entry.value, entry.value_len); |
| 126 } |
| 127 return header_table_.GetEntry(index).value(); |
| 128 } |
| 129 |
| 130 bool HpackEncodingContext::IsReferencedAt(uint32 index) const { |
| 131 CHECK_LT(index, GetEntryCount()); |
| 132 if (index >= header_table_.GetEntryCount()) |
| 133 return false; |
| 134 return header_table_.GetEntry(index).IsReferenced(); |
| 135 } |
| 136 |
| 137 uint32 HpackEncodingContext::GetTouchCountAt(uint32 index) const { |
| 138 CHECK_LT(index, GetEntryCount()); |
| 139 if (index >= header_table_.GetEntryCount()) |
| 140 return 0; |
| 141 return header_table_.GetEntry(index).TouchCount(); |
| 142 } |
| 143 |
| 144 void HpackEncodingContext::SetReferencedAt(uint32 index, bool referenced) { |
| 145 header_table_.GetMutableEntry(index)->SetReferenced(referenced); |
| 146 } |
| 147 |
| 148 void HpackEncodingContext::AddTouchesAt(uint32 index, uint32 touch_count) { |
| 149 header_table_.GetMutableEntry(index)->AddTouches(touch_count); |
| 150 } |
| 151 |
| 152 void HpackEncodingContext::ClearTouchesAt(uint32 index) { |
| 153 header_table_.GetMutableEntry(index)->ClearTouches(); |
| 154 } |
| 155 |
| 156 void HpackEncodingContext::SetMaxSize(uint32 max_size) { |
| 157 header_table_.SetMaxSize(max_size); |
| 158 } |
| 159 |
| 160 bool HpackEncodingContext::ProcessIndexedHeader( |
| 161 uint32 index, |
| 162 int32* new_index, |
| 163 std::vector<uint32>* removed_referenced_indices) { |
| 164 if (index >= GetEntryCount()) |
| 165 return false; |
| 166 |
| 167 if (index < header_table_.GetEntryCount()) { |
| 168 *new_index = index; |
| 169 removed_referenced_indices->clear(); |
| 170 HpackEntry* entry = header_table_.GetMutableEntry(index); |
| 171 entry->SetReferenced(!entry->IsReferenced()); |
| 172 } else { |
| 173 // TODO(akalin): Make HpackEntry know about owned strings and |
| 174 // non-owned strings so that it can potentially avoid copies here. |
| 175 HpackEntry entry(GetNameAt(index), GetValueAt(index)); |
| 176 |
| 177 header_table_.TryAddEntry(entry, new_index, removed_referenced_indices); |
| 178 if (*new_index >= 0) { |
| 179 header_table_.GetMutableEntry(*new_index)->SetReferenced(true); |
| 180 } |
| 181 } |
| 182 return true; |
| 183 } |
| 184 |
| 185 bool HpackEncodingContext::ProcessLiteralHeaderWithIncrementalIndexing( |
| 186 base::StringPiece name, |
| 187 base::StringPiece value, |
| 188 int32* index, |
| 189 std::vector<uint32>* removed_referenced_indices) { |
| 190 HpackEntry entry(name, value); |
| 191 header_table_.TryAddEntry(entry, index, removed_referenced_indices); |
| 192 if (*index >= 0) { |
| 193 header_table_.GetMutableEntry(*index)->SetReferenced(true); |
| 194 } |
| 195 return true; |
| 196 } |
| 197 |
| 198 } // namespace net |
OLD | NEW |