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 3 // found in the LICENSE file. 4 5 #include "net/third_party/quiche/src/quic/tools/quic_url.h" 6 7 #include "absl/strings/string_view.h" 8 #include "net/third_party/quiche/src/common/platform/api/quiche_str_cat.h" 9 10 namespace quic { 11 12 static constexpr size_t kMaxHostNameLength = 256; 13 QuicUrl(absl::string_view url)14QuicUrl::QuicUrl(absl::string_view url) : url_(static_cast<std::string>(url)) {} 15 QuicUrl(absl::string_view url,absl::string_view default_scheme)16QuicUrl::QuicUrl(absl::string_view url, absl::string_view default_scheme) 17 : QuicUrl(url) { 18 if (url_.has_scheme()) { 19 return; 20 } 21 22 url_ = GURL(quiche::QuicheStrCat(default_scheme, "://", url)); 23 } 24 ToString() const25std::string QuicUrl::ToString() const { 26 if (IsValid()) { 27 return url_.spec(); 28 } 29 return ""; 30 } 31 IsValid() const32bool QuicUrl::IsValid() const { 33 if (!url_.is_valid() || !url_.has_scheme()) { 34 return false; 35 } 36 37 if (url_.has_host() && url_.host().length() > kMaxHostNameLength) { 38 return false; 39 } 40 41 return true; 42 } 43 HostPort() const44std::string QuicUrl::HostPort() const { 45 if (!IsValid() || !url_.has_host()) { 46 return ""; 47 } 48 49 std::string host = url_.host(); 50 int port = url_.IntPort(); 51 if (port == url::PORT_UNSPECIFIED) { 52 return host; 53 } 54 return quiche::QuicheStrCat(host, ":", port); 55 } 56 PathParamsQuery() const57std::string QuicUrl::PathParamsQuery() const { 58 if (!IsValid() || !url_.has_path()) { 59 return "/"; 60 } 61 62 return url_.PathForRequest(); 63 } 64 scheme() const65std::string QuicUrl::scheme() const { 66 if (!IsValid()) { 67 return ""; 68 } 69 70 return url_.scheme(); 71 } 72 host() const73std::string QuicUrl::host() const { 74 if (!IsValid()) { 75 return ""; 76 } 77 78 return url_.HostNoBrackets(); 79 } 80 path() const81std::string QuicUrl::path() const { 82 if (!IsValid()) { 83 return ""; 84 } 85 86 return url_.path(); 87 } 88 port() const89uint16_t QuicUrl::port() const { 90 if (!IsValid()) { 91 return 0; 92 } 93 94 int port = url_.EffectiveIntPort(); 95 if (port == url::PORT_UNSPECIFIED) { 96 return 0; 97 } 98 return port; 99 } 100 101 } // namespace quic 102