mirror of
https://github.com/servo/servo.git
synced 2025-09-29 16:19:14 +01:00
Handle URL without scheme (#30148)
* cleanup and move user input logix into servoshell * fix fmt * add more tests * rename sanitize_url and add location bar test for unix system * fmt and missing rename * check for host, fix test-error * remove println, add condtion for not-none * fmt * clean up * review update * fix build failure cause by embedder_traits::resources * add cfg target-os windows * fmt * use to_file_path() instead of path()
This commit is contained in:
parent
fe7bdb7e4b
commit
bbbdb77a7a
5 changed files with 184 additions and 24 deletions
|
@ -2,8 +2,7 @@
|
|||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
||||
|
||||
use std::env;
|
||||
use std::path::Path;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use log::warn;
|
||||
use servo::net_traits::pub_domains::is_reg_domain;
|
||||
|
@ -21,35 +20,57 @@ pub fn parse_url_or_filename(cwd: &Path, input: &str) -> Result<ServoUrl, ()> {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn get_default_url(url_opt: Option<String>) -> ServoUrl {
|
||||
pub fn get_default_url(
|
||||
url_opt: Option<&str>,
|
||||
cwd: impl AsRef<Path>,
|
||||
exists: impl FnOnce(&PathBuf) -> bool,
|
||||
) -> ServoUrl {
|
||||
// If the url is not provided, we fallback to the homepage in prefs,
|
||||
// or a blank page in case the homepage is not set either.
|
||||
let cwd = env::current_dir().unwrap();
|
||||
let mut new_url = None;
|
||||
let cmdline_url = url_opt
|
||||
.clone()
|
||||
.map(|s| s.to_string())
|
||||
.and_then(|url_string| {
|
||||
parse_url_or_filename(cwd.as_ref(), &url_string)
|
||||
.map_err(|error| {
|
||||
warn!("URL parsing failed ({:?}).", error);
|
||||
error
|
||||
})
|
||||
.ok()
|
||||
});
|
||||
|
||||
let cmdline_url = url_opt.map(|s| s.to_string()).and_then(|url_string| {
|
||||
parse_url_or_filename(&cwd, &url_string)
|
||||
.map_err(|error| {
|
||||
warn!("URL parsing failed ({:?}).", error);
|
||||
error
|
||||
})
|
||||
.ok()
|
||||
});
|
||||
if let Some(url) = cmdline_url.clone() {
|
||||
// Check if the URL path corresponds to a file
|
||||
match (url.scheme(), url.host(), url.to_file_path()) {
|
||||
("file", None, Ok(ref path)) if exists(path) => {
|
||||
new_url = cmdline_url;
|
||||
},
|
||||
_ => {},
|
||||
}
|
||||
}
|
||||
|
||||
if new_url.is_none() && !url_opt.is_none() {
|
||||
new_url = location_bar_input_to_url(url_opt.unwrap());
|
||||
}
|
||||
|
||||
let pref_url = {
|
||||
let homepage_url = pref!(shell.homepage);
|
||||
parse_url_or_filename(&cwd, &homepage_url).ok()
|
||||
parse_url_or_filename(cwd.as_ref(), &homepage_url).ok()
|
||||
};
|
||||
let blank_url = ServoUrl::parse("about:blank").ok();
|
||||
|
||||
cmdline_url.or(pref_url).or(blank_url).unwrap()
|
||||
new_url.or(pref_url).or(blank_url).unwrap()
|
||||
}
|
||||
|
||||
pub fn sanitize_url(request: &str) -> Option<ServoUrl> {
|
||||
pub fn location_bar_input_to_url(request: &str) -> Option<ServoUrl> {
|
||||
let request = request.trim();
|
||||
ServoUrl::parse(request)
|
||||
.ok()
|
||||
.or_else(|| {
|
||||
if request.contains('/') || is_reg_domain(request) {
|
||||
if request.starts_with('/') {
|
||||
ServoUrl::parse(&format!("file://{}", request)).ok()
|
||||
} else if request.contains('/') || is_reg_domain(request) {
|
||||
ServoUrl::parse(&format!("https://{}", request)).ok()
|
||||
} else {
|
||||
None
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue