QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 1 | // Copyright 2016 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 | #ifndef QUICHE_HTTP2_HPACK_HPACK_STRING_H_ |
| 6 | #define QUICHE_HTTP2_HPACK_HPACK_STRING_H_ |
| 7 | |
| 8 | // HpackString is currently a very simple container for a string, but allows us |
| 9 | // to relatively easily experiment with alternate string storage mechanisms for |
| 10 | // handling strings to be encoded with HPACK, or decoded from HPACK, such as |
| 11 | // a ref-counted string. |
| 12 | |
| 13 | #include <stddef.h> |
| 14 | |
| 15 | #include <iosfwd> |
bnc | 4790400 | 2019-08-16 11:49:48 -0700 | [diff] [blame] | 16 | #include <string> |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 17 | |
vasilvv | 015e16a | 2020-10-12 23:51:06 -0700 | [diff] [blame] | 18 | #include "absl/strings/string_view.h" |
QUICHE team | 5be974e | 2020-12-29 18:35:24 -0500 | [diff] [blame] | 19 | #include "common/platform/api/quiche_export.h" |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 20 | |
| 21 | namespace http2 { |
| 22 | |
bnc | 641ace7 | 2020-01-21 12:24:57 -0800 | [diff] [blame] | 23 | class QUICHE_EXPORT_PRIVATE HpackString { |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 24 | public: |
| 25 | explicit HpackString(const char* data); |
vasilvv | 015e16a | 2020-10-12 23:51:06 -0700 | [diff] [blame] | 26 | explicit HpackString(absl::string_view str); |
bnc | 4790400 | 2019-08-16 11:49:48 -0700 | [diff] [blame] | 27 | explicit HpackString(std::string str); |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 28 | HpackString(const HpackString& other); |
| 29 | |
| 30 | // Not sure yet whether this move ctor is required/sensible. |
| 31 | HpackString(HpackString&& other) = default; |
| 32 | |
| 33 | ~HpackString(); |
| 34 | |
| 35 | size_t size() const { return str_.size(); } |
bnc | 4790400 | 2019-08-16 11:49:48 -0700 | [diff] [blame] | 36 | const std::string& ToString() const { return str_; } |
vasilvv | 015e16a | 2020-10-12 23:51:06 -0700 | [diff] [blame] | 37 | absl::string_view ToStringPiece() const; |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 38 | |
| 39 | bool operator==(const HpackString& other) const; |
| 40 | |
vasilvv | 015e16a | 2020-10-12 23:51:06 -0700 | [diff] [blame] | 41 | bool operator==(absl::string_view str) const; |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 42 | |
| 43 | private: |
bnc | 4790400 | 2019-08-16 11:49:48 -0700 | [diff] [blame] | 44 | std::string str_; |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 45 | }; |
| 46 | |
vasilvv | 015e16a | 2020-10-12 23:51:06 -0700 | [diff] [blame] | 47 | QUICHE_EXPORT_PRIVATE bool operator==(absl::string_view a, |
bnc | 641ace7 | 2020-01-21 12:24:57 -0800 | [diff] [blame] | 48 | const HpackString& b); |
vasilvv | 015e16a | 2020-10-12 23:51:06 -0700 | [diff] [blame] | 49 | QUICHE_EXPORT_PRIVATE bool operator!=(absl::string_view a, |
bnc | 641ace7 | 2020-01-21 12:24:57 -0800 | [diff] [blame] | 50 | const HpackString& b); |
| 51 | QUICHE_EXPORT_PRIVATE bool operator!=(const HpackString& a, |
| 52 | const HpackString& b); |
| 53 | QUICHE_EXPORT_PRIVATE bool operator!=(const HpackString& a, |
vasilvv | 015e16a | 2020-10-12 23:51:06 -0700 | [diff] [blame] | 54 | absl::string_view b); |
bnc | 641ace7 | 2020-01-21 12:24:57 -0800 | [diff] [blame] | 55 | QUICHE_EXPORT_PRIVATE std::ostream& operator<<(std::ostream& out, |
| 56 | const HpackString& v); |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 57 | |
bnc | 641ace7 | 2020-01-21 12:24:57 -0800 | [diff] [blame] | 58 | struct QUICHE_EXPORT_PRIVATE HpackStringPair { |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 59 | HpackStringPair(const HpackString& name, const HpackString& value); |
vasilvv | 015e16a | 2020-10-12 23:51:06 -0700 | [diff] [blame] | 60 | HpackStringPair(absl::string_view name, absl::string_view value); |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 61 | ~HpackStringPair(); |
| 62 | |
| 63 | // Returns the size of a header entry with this name and value, per the RFC: |
| 64 | // http://httpwg.org/specs/rfc7541.html#calculating.table.size |
| 65 | size_t size() const { return 32 + name.size() + value.size(); } |
| 66 | |
bnc | 4790400 | 2019-08-16 11:49:48 -0700 | [diff] [blame] | 67 | std::string DebugString() const; |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 68 | |
| 69 | const HpackString name; |
| 70 | const HpackString value; |
| 71 | }; |
| 72 | |
bnc | 641ace7 | 2020-01-21 12:24:57 -0800 | [diff] [blame] | 73 | QUICHE_EXPORT_PRIVATE std::ostream& operator<<(std::ostream& os, |
| 74 | const HpackStringPair& p); |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 75 | |
| 76 | } // namespace http2 |
| 77 | |
| 78 | #endif // QUICHE_HTTP2_HPACK_HPACK_STRING_H_ |