pub struct Url {
pub scheme: Scheme,
pub port: Option<u16>,
pub path: BString,
/* private fields */
}
Expand description
A URL with support for specialized git related capabilities.
Additionally there is support for deserialization and serialization.
§Security Warning
URLs may contain passwords and using standard formatting will redact such password, whereas lossless serialization will contain all parts of the URL. Beware that some URls still print secrets if they use them outside of the designated password fields.
Also note that URLs that fail to parse are typically stored in the resulting error type and printed in full using its display implementation.
Fields§
§scheme: Scheme
The URL scheme.
port: Option<u16>
The port to use when connecting to a host. If None
, standard ports depending on scheme
will be used.
path: BString
The path portion of the URL, usually the location of the git repository.
§Security Warning
URLs allow paths to start with -
which makes it possible to mask command-line arguments as path which then leads to
the invocation of programs from an attacker controlled URL. See https://secure.phabricator.com/T12961 for details.
If this value is ever going to be passed to a command-line application, call Self::path_argument_safe() instead.
Implementations§
Source§impl Url
impl Url
Instantiation
Sourcepub fn from_parts(
scheme: Scheme,
user: Option<String>,
password: Option<String>,
host: Option<String>,
port: Option<u16>,
path: BString,
serialize_alternative_form: bool,
) -> Result<Self, Error>
pub fn from_parts( scheme: Scheme, user: Option<String>, password: Option<String>, host: Option<String>, port: Option<u16>, path: BString, serialize_alternative_form: bool, ) -> Result<Self, Error>
Create a new instance from the given parts, including a password, which will be validated by parsing them back.
Source§impl Url
impl Url
Builder
Sourcepub fn serialize_alternate_form(self, use_alternate_form: bool) -> Self
pub fn serialize_alternate_form(self, use_alternate_form: bool) -> Self
Enable alternate serialization for this url, e.g. file:///path
becomes /path
.
This is automatically set correctly for parsed URLs, but can be set here for urls created by constructor.
Source§impl Url
impl Url
Access
Sourcepub fn user(&self) -> Option<&str>
pub fn user(&self) -> Option<&str>
Return the username mentioned in the URL, if present.
§Security Warning
URLs allow usernames to start with -
which makes it possible to mask command-line arguments as username which then leads to
the invocation of programs from an attacker controlled URL. See https://secure.phabricator.com/T12961 for details.
If this value is ever going to be passed to a command-line application, call Self::user_argument_safe() instead.
Sourcepub fn user_as_argument(&self) -> ArgumentSafety<'_>
pub fn user_as_argument(&self) -> ArgumentSafety<'_>
Classify the username of this URL by whether it is safe to pass as a command-line argument.
Use this method instead of Self::user() if the host is going to be passed to a command-line application. If the unsafe and absent cases need not be distinguished, Self::user_argument_safe() may also be used.
Sourcepub fn user_argument_safe(&self) -> Option<&str>
pub fn user_argument_safe(&self) -> Option<&str>
Return the username of this URL if present and if it can’t be mistaken for a command-line argument.
Use this method or Self::user_as_argument() instead of Self::user() if the host is going to be passed to a command-line application. Prefer Self::user_as_argument() unless the unsafe and absent cases need not be distinguished from each other.
Sourcepub fn host(&self) -> Option<&str>
pub fn host(&self) -> Option<&str>
Return the host mentioned in the URL, if present.
§Security Warning
URLs allow hosts to start with -
which makes it possible to mask command-line arguments as host which then leads to
the invocation of programs from an attacker controlled URL. See https://secure.phabricator.com/T12961 for details.
If this value is ever going to be passed to a command-line application, call Self::host_as_argument() or Self::host_argument_safe() instead.
Sourcepub fn host_as_argument(&self) -> ArgumentSafety<'_>
pub fn host_as_argument(&self) -> ArgumentSafety<'_>
Classify the host of this URL by whether it is safe to pass as a command-line argument.
Use this method instead of Self::host() if the host is going to be passed to a command-line application. If the unsafe and absent cases need not be distinguished, Self::host_argument_safe() may also be used.
Sourcepub fn host_argument_safe(&self) -> Option<&str>
pub fn host_argument_safe(&self) -> Option<&str>
Return the host of this URL if present and if it can’t be mistaken for a command-line argument.
Use this method or Self::host_as_argument() instead of Self::host() if the host is going to be passed to a command-line application. Prefer Self::host_as_argument() unless the unsafe and absent cases need not be distinguished from each other.
Sourcepub fn path_argument_safe(&self) -> Option<&BStr>
pub fn path_argument_safe(&self) -> Option<&BStr>
Return the path of this URL if it can’t be mistaken for a command-line argument. Note that it always begins with a slash, which is ignored for this comparison.
Use this method instead of accessing Self::path directly if the path is going to be passed to a
command-line application, unless it is certain that the leading /
will always be included.
Sourcepub fn path_is_root(&self) -> bool
pub fn path_is_root(&self) -> bool
Return true if the path portion of the URL is /
.
Sourcepub fn port_or_default(&self) -> Option<u16>
pub fn port_or_default(&self) -> Option<u16>
Return the actual or default port for use according to the URL scheme. Note that there may be no default port either.
Source§impl Url
impl Url
Transformation
Sourcepub fn canonicalized(&self, current_dir: &Path) -> Result<Self, Error>
pub fn canonicalized(&self, current_dir: &Path) -> Result<Self, Error>
Turn a file URL like file://relative
into file:///root/relative
, hence it assures the URL’s path component is absolute, using
current_dir
if necessary.