mkwst | 28c7c11 | 2015-07-14 22:41:06 | [diff] [blame] | 1 | // Copyright 2015 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 URL_SCHEME_HOST_PORT_H_ |
| 6 | #define URL_SCHEME_HOST_PORT_H_ |
| 7 | |
avi | c0c6031 | 2015-12-21 21:03:50 | [diff] [blame] | 8 | #include <stdint.h> |
| 9 | |
mkwst | 28c7c11 | 2015-07-14 22:41:06 | [diff] [blame] | 10 | #include <string> |
| 11 | |
mkwst | 28c7c11 | 2015-07-14 22:41:06 | [diff] [blame] | 12 | #include "base/strings/string_piece.h" |
| 13 | #include "url/url_export.h" |
| 14 | |
| 15 | class GURL; |
| 16 | |
| 17 | namespace url { |
| 18 | |
| 19 | // This class represents a (scheme, host, port) tuple extracted from a URL. |
| 20 | // |
| 21 | // The primary purpose of this class is to represent relevant network-authority |
| 22 | // information for a URL. It is _not_ an Origin, as described in RFC 6454. In |
| 23 | // particular, it is generally NOT the right thing to use for security |
| 24 | // decisions. |
| 25 | // |
| 26 | // Instead, this class is a mechanism for simplifying URLs with standard schemes |
| 27 | // (that is, those which follow the generic syntax of RFC 3986) down to the |
| 28 | // uniquely identifying information necessary for network fetches. This makes it |
| 29 | // suitable as a cache key for a collection of active connections, for instance. |
| 30 | // It may, however, be inappropriate to use as a cache key for persistent |
| 31 | // storage associated with a host. |
| 32 | // |
| 33 | // In particular, note that: |
| 34 | // |
| 35 | // * SchemeHostPort can only represent schemes which follow the RFC 3986 syntax |
| 36 | // (e.g. those registered with GURL as "standard schemes"). Non-standard |
| 37 | // schemes such as "blob", "filesystem", "data", and "javascript" can only be |
| 38 | // represented as invalid SchemeHostPort objects. |
| 39 | // |
tyoshino | 11a7c9fe | 2015-08-19 08:51:46 | [diff] [blame] | 40 | // * For example, the "file" scheme follows the standard syntax, but it is |
| 41 | // important to note that the authority portion (host, port) is optional. |
| 42 | // URLs without an authority portion will be represented with an empty string |
| 43 | // for the host, and a port of 0 (e.g. "file:///etc/hosts" => |
| 44 | // ("file", "", 0)), and URLs with a host-only authority portion will be |
| 45 | // represented with a port of 0 (e.g. "file://example.com/etc/hosts" => |
| 46 | // ("file", "example.com", 0)). See Section 3 of RFC 3986 to better understand |
| 47 | // these constructs. |
mkwst | 28c7c11 | 2015-07-14 22:41:06 | [diff] [blame] | 48 | // |
| 49 | // * SchemeHostPort has no notion of the Origin concept (RFC 6454), and in |
| 50 | // particular, it has no notion of a "unique" Origin. If you need to take |
| 51 | // uniqueness into account (and, if you're making security-relevant decisions |
tyoshino | 11a7c9fe | 2015-08-19 08:51:46 | [diff] [blame] | 52 | // then you absolutely do), please use 'url::Origin' instead. |
mkwst | 28c7c11 | 2015-07-14 22:41:06 | [diff] [blame] | 53 | // |
| 54 | // Usage: |
| 55 | // |
| 56 | // * SchemeHostPort objects are commonly created from GURL objects: |
| 57 | // |
| 58 | // GURL url("https://example.com/"); |
| 59 | // url::SchemeHostPort tuple(url); |
| 60 | // tuple.scheme(); // "https" |
| 61 | // tuple.host(); // "example.com" |
| 62 | // tuple.port(); // 443 |
| 63 | // |
| 64 | // * Objects may also be explicitly created and compared: |
| 65 | // |
| 66 | // url::SchemeHostPort tuple(url::kHttpsScheme, "example.com", 443); |
| 67 | // tuple.scheme(); // "https" |
| 68 | // tuple.host(); // "example.com" |
| 69 | // tuple.port(); // 443 |
| 70 | // |
| 71 | // GURL url("https://example.com/"); |
| 72 | // tuple.Equals(url::SchemeHostPort(url)); // true |
| 73 | class URL_EXPORT SchemeHostPort { |
| 74 | public: |
| 75 | // Creates an invalid (scheme, host, port) tuple, which represents an invalid |
| 76 | // or non-standard URL. |
| 77 | SchemeHostPort(); |
| 78 | |
| 79 | // Creates a (scheme, host, port) tuple. |host| must be a canonicalized |
| 80 | // A-label (that is, '☃.net' must be provided as 'xn--n3h.net'). |scheme| |
| 81 | // must be a standard scheme. |port| must not be 0, unless |scheme| does not |
| 82 | // support ports (e.g. 'file'). In that case, |port| must be 0. |
| 83 | // |
| 84 | // Copies the data in |scheme| and |host|. |
avi | c0c6031 | 2015-12-21 21:03:50 | [diff] [blame] | 85 | SchemeHostPort(base::StringPiece scheme, |
| 86 | base::StringPiece host, |
| 87 | uint16_t port); |
mkwst | 28c7c11 | 2015-07-14 22:41:06 | [diff] [blame] | 88 | |
| 89 | // Creates a (scheme, host, port) tuple from |url|, as described at |
| 90 | // https://tools.ietf.org/html/rfc6454#section-4 |
| 91 | // |
| 92 | // If |url| is invalid or non-standard, the result will be an invalid |
| 93 | // SchemeHostPort object. |
| 94 | explicit SchemeHostPort(const GURL& url); |
| 95 | |
| 96 | ~SchemeHostPort(); |
| 97 | |
| 98 | // Returns the host component, in URL form. That is all IDN domain names will |
| 99 | // be expressed as A-Labels ('☃.net' will be returned as 'xn--n3h.net'), and |
| 100 | // and all IPv6 addresses will be enclosed in brackets ("[2001:db8::1]"). |
mkwst | f5fef06 | 2015-07-22 08:29:01 | [diff] [blame] | 101 | const std::string& host() const { return host_; } |
| 102 | const std::string& scheme() const { return scheme_; } |
avi | c0c6031 | 2015-12-21 21:03:50 | [diff] [blame] | 103 | uint16_t port() const { return port_; } |
mkwst | 28c7c11 | 2015-07-14 22:41:06 | [diff] [blame] | 104 | bool IsInvalid() const; |
| 105 | |
| 106 | // Serializes the SchemeHostPort tuple to a canonical form. |
| 107 | // |
| 108 | // While this string form resembles the Origin serialization specified in |
| 109 | // Section 6.2 of RFC 6454, it is important to note that invalid |
| 110 | // SchemeHostPort tuples serialize to the empty string, rather than being |
| 111 | // serialized as a unique Origin. |
| 112 | std::string Serialize() const; |
| 113 | |
| 114 | // Two SchemeHostPort objects are "equal" iff their schemes, hosts, and ports |
| 115 | // are exact matches. |
| 116 | // |
| 117 | // Note that this comparison is _not_ the same as an origin-based comparison. |
| 118 | // In particular, invalid SchemeHostPort objects match each other (and |
| 119 | // themselves). Unique origins, on the other hand, would not. |
| 120 | bool Equals(const SchemeHostPort& other) const; |
| 121 | |
nick | 1466c84 | 2015-11-25 20:08:06 | [diff] [blame] | 122 | // Allows SchemeHostPort to be used as a key in STL (for example, a std::set |
| 123 | // or std::map). |
mkwst | 28c7c11 | 2015-07-14 22:41:06 | [diff] [blame] | 124 | bool operator<(const SchemeHostPort& other) const; |
| 125 | |
| 126 | private: |
| 127 | std::string scheme_; |
| 128 | std::string host_; |
avi | c0c6031 | 2015-12-21 21:03:50 | [diff] [blame] | 129 | uint16_t port_; |
mkwst | 28c7c11 | 2015-07-14 22:41:06 | [diff] [blame] | 130 | }; |
| 131 | |
| 132 | } // namespace url |
| 133 | |
| 134 | #endif // URL_SCHEME_HOST_PORT_H_ |