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 #ifndef QUICHE_QUIC_CORE_QUIC_SERVER_ID_H_ 6 #define QUICHE_QUIC_CORE_QUIC_SERVER_ID_H_ 7 8 #include <cstdint> 9 #include <string> 10 11 #include "absl/hash/hash.h" 12 #include "absl/strings/string_view.h" 13 #include "absl/types/optional.h" 14 #include "quiche/quic/platform/api/quic_export.h" 15 16 namespace quic { 17 18 // The id used to identify sessions. Includes the hostname, port, scheme and 19 // privacy_mode. 20 class QUIC_EXPORT_PRIVATE QuicServerId { 21 public: 22 // Attempts to parse a QuicServerId from a "host:port" string. Returns nullopt 23 // if input could not be parsed. Requires input to contain both host and port 24 // and no other components of a URL authority. 25 static absl::optional<QuicServerId> ParseFromHostPortString( 26 absl::string_view host_port_string); 27 28 QuicServerId(); 29 QuicServerId(std::string host, uint16_t port); 30 QuicServerId(std::string host, uint16_t port, bool privacy_mode_enabled); 31 ~QuicServerId(); 32 33 // Needed to be an element of an ordered container. 34 bool operator<(const QuicServerId& other) const; 35 bool operator==(const QuicServerId& other) const; 36 37 bool operator!=(const QuicServerId& other) const; 38 host()39 const std::string& host() const { return host_; } 40 port()41 uint16_t port() const { return port_; } 42 privacy_mode_enabled()43 bool privacy_mode_enabled() const { return privacy_mode_enabled_; } 44 45 // Returns a "host:port" representation. IPv6 literal hosts will always be 46 // bracketed in result. 47 std::string ToHostPortString() const; 48 49 // If host is an IPv6 literal surrounded by [], returns the substring without 50 // []. Otherwise, returns host as is. 51 absl::string_view GetHostWithoutIpv6Brackets() const; 52 53 // If host is an IPv6 literal without surrounding [], returns host wrapped in 54 // []. Otherwise, returns host as is. 55 std::string GetHostWithIpv6Brackets() const; 56 57 template <typename H> AbslHashValue(H h,const QuicServerId & server_id)58 friend H AbslHashValue(H h, const QuicServerId& server_id) { 59 return H::combine(std::move(h), server_id.host(), server_id.port(), 60 server_id.privacy_mode_enabled()); 61 } 62 63 private: 64 std::string host_; 65 uint16_t port_; 66 bool privacy_mode_enabled_; 67 }; 68 69 using QuicServerIdHash = absl::Hash<QuicServerId>; 70 71 } // namespace quic 72 73 #endif // QUICHE_QUIC_CORE_QUIC_SERVER_ID_H_ 74