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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
|
//! URI parsing and manipulation.
use crate::pool::Pool;
pub use apr_sys::apr_uri_t;
use std::ffi::CStr;
use std::marker::PhantomData;
/// A structure to represent a URI.
#[derive(Debug)]
pub struct Uri<'pool> {
ptr: *mut apr_uri_t,
_pool: PhantomData<&'pool Pool>,
}
impl<'pool> Uri<'pool> {
/// Return the scheme of the URI.
pub fn scheme(&self) -> Option<&str> {
unsafe {
if (*self.ptr).scheme.is_null() {
None
} else {
Some(CStr::from_ptr((*self.ptr).scheme).to_str().unwrap())
}
}
}
/// Return the hostinfo of the URI.
pub fn hostinfo(&self) -> Option<&str> {
unsafe {
if (*self.ptr).hostinfo.is_null() {
None
} else {
Some(CStr::from_ptr((*self.ptr).hostinfo).to_str().unwrap())
}
}
}
/// Return the username of the URI.
pub fn user(&self) -> Option<&str> {
unsafe {
if (*self.ptr).user.is_null() {
None
} else {
Some(CStr::from_ptr((*self.ptr).user).to_str().unwrap())
}
}
}
/// Return the password of the URI.
pub fn password(&self) -> Option<&str> {
unsafe {
if (*self.ptr).password.is_null() {
None
} else {
Some(CStr::from_ptr((*self.ptr).password).to_str().unwrap())
}
}
}
/// Return the hostname of the URI.
pub fn hostname(&self) -> Option<&str> {
unsafe {
if (*self.ptr).hostname.is_null() {
None
} else {
Some(CStr::from_ptr((*self.ptr).hostname).to_str().unwrap())
}
}
}
/// Return the port of the URI.
pub fn port(&self) -> u16 {
unsafe { (*self.ptr).port }
}
/// Return the path of the URI.
pub fn path(&self) -> Option<&str> {
unsafe {
if (*self.ptr).path.is_null() {
None
} else {
Some(CStr::from_ptr((*self.ptr).path).to_str().unwrap())
}
}
}
/// Return the query of the URI.
pub fn query(&self) -> Option<&str> {
unsafe {
if (*self.ptr).query.is_null() {
None
} else {
Some(CStr::from_ptr((*self.ptr).query).to_str().unwrap())
}
}
}
/// Return the fragment of the URI.
pub fn fragment(&self) -> Option<&str> {
unsafe {
if (*self.ptr).fragment.is_null() {
None
} else {
Some(CStr::from_ptr((*self.ptr).fragment).to_str().unwrap())
}
}
}
/// Return the port as a string
pub fn port_str(&self) -> Option<&str> {
unsafe {
if (*self.ptr).port_str.is_null() {
None
} else {
Some(CStr::from_ptr((*self.ptr).port_str).to_str().unwrap())
}
}
}
/// Return whether the URI has been initialized.
pub fn is_initialized(&self) -> bool {
unsafe { (*self.ptr).is_initialized() != 0 }
}
/// Return whether the DNS has been looked up.
pub fn dns_looked_up(&self) -> bool {
unsafe { (*self.ptr).dns_looked_up() != 0 }
}
/// Return whether the DNS has been resolved.
pub fn dns_resolved(&self) -> bool {
unsafe { (*self.ptr).dns_resolved() != 0 }
}
/// Unparse the URI, returning a string.
pub fn unparse(&self, flags: u32) -> String {
let pool = crate::Pool::new();
unsafe {
CStr::from_ptr(apr_sys::apr_uri_unparse(pool.as_mut_ptr(), self.ptr, flags))
.to_str()
.unwrap()
}
.to_string()
}
/// Parse a hostinfo string.
pub fn parse_hostinfo(pool: &'pool Pool, hostinfo: &str) -> Result<Self, crate::Status> {
let uri = pool.calloc::<apr_uri_t>();
let hostinfo = std::ffi::CString::new(hostinfo).unwrap();
let status = unsafe {
apr_sys::apr_uri_parse_hostinfo(
pool.as_mut_ptr(),
hostinfo.as_ptr() as *const std::ffi::c_char,
uri,
)
};
let status = crate::Status::from(status);
if status.is_success() {
Ok(Uri {
ptr: uri,
_pool: PhantomData,
})
} else {
Err(status)
}
}
/// Parse a URI string.
pub fn parse(pool: &'pool Pool, url: &str) -> Result<Self, crate::Status> {
let uri = pool.calloc::<apr_uri_t>();
let url = std::ffi::CString::new(url).unwrap();
let status = unsafe {
apr_sys::apr_uri_parse(
pool.as_mut_ptr(),
url.as_ptr() as *const std::ffi::c_char,
uri,
)
};
let status = crate::Status::from(status);
if status.is_success() {
Ok(Uri {
ptr: uri,
_pool: PhantomData,
})
} else {
Err(status)
}
}
/// Get a raw pointer to the underlying apr_uri_t
pub fn as_ptr(&self) -> *const apr_uri_t {
self.ptr
}
/// Get a mutable raw pointer to the underlying apr_uri_t
///
/// # Safety
///
/// The caller must ensure that any modifications made through the returned
/// pointer are valid and do not violate APR's invariants.
pub unsafe fn as_mut_ptr(&mut self) -> *mut apr_uri_t {
self.ptr
}
}
// Add Display implementation
impl<'pool> std::fmt::Display for Uri<'pool> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.unparse(0))
}
}
// Add PartialEq implementation
impl<'pool> PartialEq for Uri<'pool> {
fn eq(&self, other: &Self) -> bool {
self.unparse(0) == other.unparse(0)
}
}
// Add AsRef implementations for easy access to string parts
impl<'pool> AsRef<str> for Uri<'pool> {
fn as_ref(&self) -> &str {
self.scheme().unwrap_or("")
}
}
/// Return the default port for a given scheme.
pub fn port_of_scheme(scheme: &str) -> u16 {
let scheme = std::ffi::CString::new(scheme).unwrap();
unsafe { apr_sys::apr_uri_port_of_scheme(scheme.as_ptr() as *const std::ffi::c_char) }
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_port_of_scheme() {
assert_eq!(80, super::port_of_scheme("http"));
assert_eq!(443, super::port_of_scheme("https"));
assert_eq!(0, super::port_of_scheme("unknown"));
}
#[test]
fn test_parse() {
let pool = Pool::new();
let uri = super::Uri::parse(&pool, "http://example.com:8080/").unwrap();
assert_eq!("http", uri.scheme().unwrap());
assert_eq!(Some("example.com:8080"), uri.hostinfo());
assert_eq!(Some("example.com"), uri.hostname());
assert_eq!(8080, uri.port());
assert_eq!(Some("/"), uri.path());
assert_eq!(None, uri.query());
assert_eq!(None, uri.fragment());
assert_eq!(Some("8080"), uri.port_str());
assert!(uri.is_initialized());
assert!(!uri.dns_looked_up());
assert!(!uri.dns_resolved());
}
#[test]
fn test_parse_hostinfo() {
let pool = Pool::new();
let uri = super::Uri::parse_hostinfo(&pool, "example.com:8080").unwrap();
assert_eq!(None, uri.scheme());
assert_eq!(Some("example.com:8080"), uri.hostinfo());
assert_eq!(Some("example.com"), uri.hostname());
assert_eq!(8080, uri.port());
assert_eq!(None, uri.path());
assert_eq!(None, uri.query());
assert_eq!(None, uri.fragment());
assert_eq!(Some("8080"), uri.port_str());
assert!(uri.is_initialized());
assert!(!uri.dns_looked_up());
assert!(!uri.dns_resolved());
}
}
// TODO(jelmer): Rather than serializing/deserializing, we should be able to just copy the fields
// over.
// Note: This impl cannot work without a pool parameter, so it's commented out
// #[cfg(feature = "url")]
// impl From<url::Url> for Uri {
// fn from(url: url::Url) -> Self {
// let s = url.as_str();
// Self::parse(s).unwrap()
// }
// }
#[cfg(feature = "url")]
impl<'a> From<Uri<'a>> for url::Url {
fn from(uri: Uri<'a>) -> Self {
let s = uri.unparse(0);
url::Url::parse(&s).unwrap()
}
}
|