blob: 4094ffebb7e555006499a354475ef562cef94b6c [file] [log] [blame]
QUICHE teama6ef0a62019-03-07 20:34:33 -05001// 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 "net/third_party/quiche/src/quic/platform/api/quic_str_cat.h"
8#include "net/third_party/quiche/src/quic/platform/api/quic_text_utils.h"
9
10namespace quic {
11
12static constexpr size_t kMaxHostNameLength = 256;
13
vasilvvc48c8712019-03-11 13:38:16 -070014QuicUrl::QuicUrl(QuicStringPiece url) : url_(static_cast<std::string>(url)) {}
QUICHE teama6ef0a62019-03-07 20:34:33 -050015
16QuicUrl::QuicUrl(QuicStringPiece url, QuicStringPiece default_scheme)
17 : QuicUrl(url) {
18 if (url_.has_scheme()) {
19 return;
20 }
21
22 url_ = GURL(QuicStrCat(default_scheme, "://", url));
23}
24
vasilvvc48c8712019-03-11 13:38:16 -070025std::string QuicUrl::ToString() const {
QUICHE teama6ef0a62019-03-07 20:34:33 -050026 if (IsValid()) {
27 return url_.spec();
28 }
29 return "";
30}
31
32bool 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
vasilvvc48c8712019-03-11 13:38:16 -070044std::string QuicUrl::HostPort() const {
QUICHE teama6ef0a62019-03-07 20:34:33 -050045 if (!IsValid() || !url_.has_host()) {
46 return "";
47 }
48
vasilvvc48c8712019-03-11 13:38:16 -070049 std::string host = url_.host();
QUICHE teama6ef0a62019-03-07 20:34:33 -050050 int port = url_.IntPort();
51 if (port == url::PORT_UNSPECIFIED) {
52 return host;
53 }
54 return QuicStrCat(host, ":", port);
55}
56
vasilvvc48c8712019-03-11 13:38:16 -070057std::string QuicUrl::PathParamsQuery() const {
QUICHE teama6ef0a62019-03-07 20:34:33 -050058 if (!IsValid() || !url_.has_path()) {
59 return "/";
60 }
61
62 return url_.PathForRequest();
63}
64
vasilvvc48c8712019-03-11 13:38:16 -070065std::string QuicUrl::scheme() const {
QUICHE teama6ef0a62019-03-07 20:34:33 -050066 if (!IsValid()) {
67 return "";
68 }
69
70 return url_.scheme();
71}
72
vasilvvc48c8712019-03-11 13:38:16 -070073std::string QuicUrl::host() const {
QUICHE teama6ef0a62019-03-07 20:34:33 -050074 if (!IsValid()) {
75 return "";
76 }
77
78 return url_.HostNoBrackets();
79}
80
vasilvvc48c8712019-03-11 13:38:16 -070081std::string QuicUrl::path() const {
QUICHE teama6ef0a62019-03-07 20:34:33 -050082 if (!IsValid()) {
83 return "";
84 }
85
86 return url_.path();
87}
88
89uint16_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