-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_url.rs
143 lines (135 loc) · 3.72 KB
/
parse_url.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
use std::io::{Error, ErrorKind};
use url::{Host, Origin, ParseError, Position::AfterPath, Url};
/// Parses a URL from a string.
///
/// # Arguments
///
/// * `string_url` - The URL to parse.
///
/// # Example
///
/// ```
/// use web_programming::parse_url_from_string;
///
/// let string_url = "https://www.rust-lang.org/";
/// let result = parse_url_from_string(string_url);
/// assert!(result.is_ok());
/// ```
pub fn parse_url_from_string(string_url: &str) -> Result<Url, ParseError> {
Url::parse(string_url)
}
/// Gets the base URL of a URL.
///
/// # Arguments
///
/// * `full_url` - The URL string to get the base URL from.
///
/// # Example
///
/// ```
/// use web_programming::get_base_url;
///
/// let full_url = "https://github.com/rust-lang/cargo?asdf";
/// let base = get_base_url(full_url).unwrap();
/// assert_eq!(base.as_str(), "https://github.com/");
/// ```
pub fn get_base_url(full_url: &str) -> Result<Url, Error> {
let mut url = Url::parse(full_url).unwrap();
match url.path_segments_mut() {
Ok(mut path) => {
path.clear();
}
Err(_) => {
return Err(Error::new(ErrorKind::InvalidInput, "Cannot be a base URL"));
}
}
url.set_query(None);
Ok(url)
}
/// Creates a URL from a base URL and a path.
///
/// # Arguments
///
/// * `base_url` - The base URL.
/// * `path` - The path to append to the base URL.
///
/// # Example
///
/// ```
/// use web_programming::create_urls_from_base_url;
/// use url::Url;
///
/// let base_url = "https://www.rust-lang.org/";
/// let path = "learn";
/// let result = create_urls_from_base_url(base_url, path);
/// assert!(result.is_ok());
/// ```
pub fn create_urls_from_base_url(base_url: &str, path: &str) -> Result<Url, ParseError> {
let base = Url::parse(base_url).expect("hardcoded URL is known to be valid");
base.join(path)
}
/// Extracts the URL origin from a URL.
///
/// # Arguments
///
/// * `string_url` - The URL to extract the origin from.
///
/// # Example
///
/// ```
/// use web_programming::extract_url_origin;
/// use url::Host;
///
/// let string_url = "https://www.rust-lang.org/learn";
/// let result = extract_url_origin(string_url);
/// assert!(result.is_ok());
/// ```
pub fn extract_url_origin(string_url: &str) -> Result<(String, Host<String>, u16), Error> {
let url = Url::parse(string_url).unwrap();
let scheme = url.scheme().to_string();
let host = url.host().unwrap().to_string();
let port = url.port_or_known_default().unwrap();
Ok((scheme, Host::parse(&host).unwrap(), port))
}
/// Extracts the URL origin from a URL.
///
///
/// # Arguments
///
/// * `string_url` - The URL to extract the origin from.
///
/// # Example
///
/// ```
/// use web_programming::extract_url_origin_alt;
/// use url::Origin;
///
/// let string_url = "ftp://rust-lang.org/examples";
/// let result = extract_url_origin_alt(string_url);
/// assert!(result.is_ok());
/// ```
pub fn extract_url_origin_alt(string_url: &str) -> Result<Origin, ParseError> {
let url = Url::parse(string_url).unwrap();
let result = url.origin();
Ok(result)
}
/// Removes fragment identifiers and query pairs from a URL
///
/// /// # Arguments
///
/// * `string_url` - The URL to extract the origin from.
///
/// # Example
///
/// ```
/// use web_programming::remove_fragment_identifiers_and_query_pairs;
///
/// let string_url = "https://www.rust-lang.org/learn?query=string#frag";
/// let result = remove_fragment_identifiers_and_query_pairs(string_url).unwrap();
/// assert_eq!(result, "https://www.rust-lang.org/learn");
/// ```
pub fn remove_fragment_identifiers_and_query_pairs(string_url: &str) -> Result<String, ParseError> {
let parsed = Url::parse(string_url).unwrap();
let cleaned = parsed[..AfterPath].to_string();
Ok(cleaned)
}