OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #ifndef NET_SPDY_SPDY_HEADER_BLOCK_H_ | 5 #ifndef NET_SPDY_SPDY_HEADER_BLOCK_H_ |
6 #define NET_SPDY_SPDY_HEADER_BLOCK_H_ | 6 #define NET_SPDY_SPDY_HEADER_BLOCK_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
| 9 #include <memory> |
9 #include <string> | 10 #include <string> |
10 | 11 |
| 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/strings/string_piece.h" |
| 14 #include "net/base/linked_hash_map.h" |
11 #include "net/base/net_export.h" | 15 #include "net/base/net_export.h" |
12 #include "net/log/net_log.h" | 16 #include "net/log/net_log.h" |
13 | 17 |
14 namespace net { | 18 namespace net { |
15 | 19 |
16 // A data structure for holding a set of headers from either a | 20 // Allows arg-dependent lookup to work for logging's operator<<. |
17 // SYN_STREAM or SYN_REPLY frame. | 21 using ::operator<<; |
18 typedef std::map<std::string, std::string> SpdyHeaderBlock; | 22 |
| 23 // This class provides a key-value map that can be used to store SPDY header |
| 24 // names and values. This data structure preserves insertion order. |
| 25 // |
| 26 // Under the hood, this data structure uses large, contiguous blocks of memory |
| 27 // to store names and values. Lookups may be performed with StringPiece keys, |
| 28 // and values are returned as StringPieces (via StringPieceProxy, below). |
| 29 // Value StringPieces are valid as long as the SpdyHeaderBlock exists; allocated |
| 30 // memory is never freed until SpdyHeaderBlock's destruction. |
| 31 // |
| 32 // This implementation does not make much of an effort to minimize wasted space. |
| 33 // It's expected that keys are rarely deleted from a SpdyHeaderBlock. |
| 34 class NET_EXPORT SpdyHeaderBlock { |
| 35 private: |
| 36 using MapType = linked_hash_map<base::StringPiece, base::StringPiece>; |
| 37 class Storage; |
| 38 |
| 39 public: |
| 40 using iterator = MapType::iterator; |
| 41 using const_iterator = MapType::const_iterator; |
| 42 using value_type = MapType::value_type; |
| 43 using reverse_iterator = MapType::reverse_iterator; |
| 44 |
| 45 class StringPieceProxy; |
| 46 |
| 47 SpdyHeaderBlock(); |
| 48 SpdyHeaderBlock(const SpdyHeaderBlock& other); |
| 49 ~SpdyHeaderBlock(); |
| 50 |
| 51 SpdyHeaderBlock& operator=(const SpdyHeaderBlock& other); |
| 52 |
| 53 // These methods delegate to our MapType member. |
| 54 iterator begin() { return block_.begin(); } |
| 55 iterator end() { return block_.end(); } |
| 56 const_iterator begin() const { return block_.begin(); } |
| 57 const_iterator end() const { return block_.end(); } |
| 58 bool empty() const { return block_.empty(); } |
| 59 size_t size() const { return block_.size(); } |
| 60 iterator find(base::StringPiece key) { return block_.find(key); } |
| 61 const_iterator find(base::StringPiece key) const { return block_.find(key); } |
| 62 reverse_iterator rbegin() { return block_.rbegin(); } |
| 63 void erase(base::StringPiece key) { block_.erase(key); } |
| 64 |
| 65 // Clears both our MapType member and the memory used to hold headers. |
| 66 void clear(); |
| 67 |
| 68 // These methods copy data into our backing storage. |
| 69 void insert(const MapType::value_type& value); |
| 70 void ReplaceOrAppendHeader(const base::StringPiece key, |
| 71 const base::StringPiece value); |
| 72 |
| 73 // Allows either lookup or mutation of the value associated with a key. |
| 74 StringPieceProxy operator[](const base::StringPiece key); |
| 75 |
| 76 bool operator==(const SpdyHeaderBlock& other) const; |
| 77 |
| 78 // This object provides automatic conversions that allow SpdyHeaderBlock to be |
| 79 // nearly a drop-in replacement for linked_hash_map<string, string>. It reads |
| 80 // data from or writes data to a SpdyHeaderBlock::Storage. |
| 81 class NET_EXPORT StringPieceProxy { |
| 82 public: |
| 83 ~StringPieceProxy(); |
| 84 |
| 85 // Assignment modifies the underlying SpdyHeaderBlock. |
| 86 StringPieceProxy& operator=(const base::StringPiece other); |
| 87 |
| 88 // Allows a StringPieceProxy to be automatically converted to a StringPiece. |
| 89 // This makes SpdyHeaderBlock::operator[] easy to use with StringPieces. |
| 90 operator base::StringPiece() const; |
| 91 |
| 92 // Reserves |size| bytes in the underlying storage. |
| 93 void reserve(size_t size); |
| 94 |
| 95 std::string as_string() const { |
| 96 return static_cast<base::StringPiece>(*this).as_string(); |
| 97 } |
| 98 |
| 99 private: |
| 100 friend class SpdyHeaderBlock; |
| 101 |
| 102 StringPieceProxy(SpdyHeaderBlock::MapType* block, |
| 103 SpdyHeaderBlock::Storage* storage, |
| 104 SpdyHeaderBlock::MapType::iterator lookup_result, |
| 105 const base::StringPiece key); |
| 106 |
| 107 SpdyHeaderBlock::MapType* block_; |
| 108 SpdyHeaderBlock::Storage* storage_; |
| 109 SpdyHeaderBlock::MapType::iterator lookup_result_; |
| 110 const base::StringPiece key_; |
| 111 |
| 112 // Contains only POD members; explicitly copyable. |
| 113 }; |
| 114 |
| 115 private: |
| 116 void Write(const base::StringPiece s); |
| 117 void AppendHeader(const base::StringPiece key, const base::StringPiece value); |
| 118 |
| 119 MapType block_; |
| 120 scoped_ptr<Storage> storage_; |
| 121 }; |
19 | 122 |
20 // Converts a SpdyHeaderBlock into NetLog event parameters. | 123 // Converts a SpdyHeaderBlock into NetLog event parameters. |
21 NET_EXPORT scoped_ptr<base::Value> SpdyHeaderBlockNetLogCallback( | 124 NET_EXPORT scoped_ptr<base::Value> SpdyHeaderBlockNetLogCallback( |
22 const SpdyHeaderBlock* headers, | 125 const SpdyHeaderBlock* headers, |
23 NetLogCaptureMode capture_mode); | 126 NetLogCaptureMode capture_mode); |
24 | 127 |
25 // Converts NetLog event parameters into a SPDY header block and writes them | 128 // Converts NetLog event parameters into a SPDY header block and writes them |
26 // to |headers|. |event_param| must have been created by | 129 // to |headers|. |event_param| must have been created by |
27 // SpdyHeaderBlockNetLogCallback. On failure, returns false and clears | 130 // SpdyHeaderBlockNetLogCallback. On failure, returns false and clears |
28 // |headers|. | 131 // |headers|. |
29 NET_EXPORT bool SpdyHeaderBlockFromNetLogParam( | 132 NET_EXPORT bool SpdyHeaderBlockFromNetLogParam( |
30 const base::Value* event_param, | 133 const base::Value* event_param, |
31 SpdyHeaderBlock* headers); | 134 SpdyHeaderBlock* headers); |
32 | 135 |
33 } // namespace net | 136 } // namespace net |
34 | 137 |
35 #endif // NET_SPDY_SPDY_HEADER_BLOCK_H_ | 138 #endif // NET_SPDY_SPDY_HEADER_BLOCK_H_ |
OLD | NEW |