blob: 78960467c992c6b1d249f68ce109edaa096614ed [file] [log] [blame]
QUICHE teamfd50a402018-12-07 22:54:05 -05001// 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>
bnc47904002019-08-16 11:49:48 -070016#include <string>
QUICHE teamfd50a402018-12-07 22:54:05 -050017
vasilvv015e16a2020-10-12 23:51:06 -070018#include "absl/strings/string_view.h"
QUICHE team5be974e2020-12-29 18:35:24 -050019#include "common/platform/api/quiche_export.h"
QUICHE teamfd50a402018-12-07 22:54:05 -050020
21namespace http2 {
22
bnc641ace72020-01-21 12:24:57 -080023class QUICHE_EXPORT_PRIVATE HpackString {
QUICHE teamfd50a402018-12-07 22:54:05 -050024 public:
25 explicit HpackString(const char* data);
vasilvv015e16a2020-10-12 23:51:06 -070026 explicit HpackString(absl::string_view str);
bnc47904002019-08-16 11:49:48 -070027 explicit HpackString(std::string str);
QUICHE teamfd50a402018-12-07 22:54:05 -050028 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(); }
bnc47904002019-08-16 11:49:48 -070036 const std::string& ToString() const { return str_; }
vasilvv015e16a2020-10-12 23:51:06 -070037 absl::string_view ToStringPiece() const;
QUICHE teamfd50a402018-12-07 22:54:05 -050038
39 bool operator==(const HpackString& other) const;
40
vasilvv015e16a2020-10-12 23:51:06 -070041 bool operator==(absl::string_view str) const;
QUICHE teamfd50a402018-12-07 22:54:05 -050042
43 private:
bnc47904002019-08-16 11:49:48 -070044 std::string str_;
QUICHE teamfd50a402018-12-07 22:54:05 -050045};
46
vasilvv015e16a2020-10-12 23:51:06 -070047QUICHE_EXPORT_PRIVATE bool operator==(absl::string_view a,
bnc641ace72020-01-21 12:24:57 -080048 const HpackString& b);
vasilvv015e16a2020-10-12 23:51:06 -070049QUICHE_EXPORT_PRIVATE bool operator!=(absl::string_view a,
bnc641ace72020-01-21 12:24:57 -080050 const HpackString& b);
51QUICHE_EXPORT_PRIVATE bool operator!=(const HpackString& a,
52 const HpackString& b);
53QUICHE_EXPORT_PRIVATE bool operator!=(const HpackString& a,
vasilvv015e16a2020-10-12 23:51:06 -070054 absl::string_view b);
bnc641ace72020-01-21 12:24:57 -080055QUICHE_EXPORT_PRIVATE std::ostream& operator<<(std::ostream& out,
56 const HpackString& v);
QUICHE teamfd50a402018-12-07 22:54:05 -050057
bnc641ace72020-01-21 12:24:57 -080058struct QUICHE_EXPORT_PRIVATE HpackStringPair {
QUICHE teamfd50a402018-12-07 22:54:05 -050059 HpackStringPair(const HpackString& name, const HpackString& value);
vasilvv015e16a2020-10-12 23:51:06 -070060 HpackStringPair(absl::string_view name, absl::string_view value);
QUICHE teamfd50a402018-12-07 22:54:05 -050061 ~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
bnc47904002019-08-16 11:49:48 -070067 std::string DebugString() const;
QUICHE teamfd50a402018-12-07 22:54:05 -050068
69 const HpackString name;
70 const HpackString value;
71};
72
bnc641ace72020-01-21 12:24:57 -080073QUICHE_EXPORT_PRIVATE std::ostream& operator<<(std::ostream& os,
74 const HpackStringPair& p);
QUICHE teamfd50a402018-12-07 22:54:05 -050075
76} // namespace http2
77
78#endif // QUICHE_HTTP2_HPACK_HPACK_STRING_H_