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 295 296 297 298 299 300
|
/* -*- Mode: rust; rust-indent-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
extern crate urlpattern;
use urlpattern::quirks as Uq;
extern crate nsstring;
use nsstring::nsACString;
use nsstring::nsCString;
use thin_vec::ThinVec;
mod helpers;
use helpers::*;
pub mod base;
use base::*;
use log::debug;
#[no_mangle]
pub extern "C" fn urlp_parse_pattern_from_string(
input: *const nsACString,
base_url: *const nsACString,
options: UrlpOptions,
res: *mut UrlpPattern,
) -> bool {
debug!("urlp_parse_pattern_from_string()");
let init = if let Some(init) = init_from_string_and_base_url(input, base_url) {
init
} else {
return false;
};
if let Ok(pattern) = Uq::parse_pattern(init, options.into()) {
unsafe {
*res = UrlpPattern(Box::into_raw(Box::new(pattern)) as *mut _);
}
return true;
}
false
}
#[no_mangle]
pub unsafe extern "C" fn urlp_parse_pattern_from_init(
init: &UrlpInit,
options: UrlpOptions,
res: *mut UrlpPattern,
) -> bool {
debug!("urlp_parse_pattern_from_init()");
if let Ok(pattern) = Uq::parse_pattern(init.into(), options.into()) {
*res = UrlpPattern(Box::into_raw(Box::new(pattern)) as *mut _);
return true;
}
false
}
#[no_mangle]
pub unsafe extern "C" fn urlp_pattern_free(pattern: UrlpPattern) {
drop(Box::from_raw(pattern.0 as *mut Uq::UrlPattern));
}
#[no_mangle]
pub unsafe extern "C" fn urlp_get_protocol_component(
pattern: UrlpPattern,
res: *mut UrlpComponent,
) {
let q_pattern = &*(pattern.0 as *const Uq::UrlPattern);
let tmp: UrlpComponent = q_pattern.protocol.clone().into();
*res = tmp;
}
#[no_mangle]
pub unsafe extern "C" fn urlp_get_username_component(
pattern: UrlpPattern,
res: *mut UrlpComponent,
) {
let q_pattern = &*(pattern.0 as *const Uq::UrlPattern);
let tmp: UrlpComponent = q_pattern.username.clone().into();
*res = tmp;
}
#[no_mangle]
pub unsafe extern "C" fn urlp_get_password_component(
pattern: UrlpPattern,
res: *mut UrlpComponent,
) {
let q_pattern = &*(pattern.0 as *const Uq::UrlPattern);
let tmp: UrlpComponent = q_pattern.password.clone().into();
*res = tmp;
}
#[no_mangle]
pub unsafe extern "C" fn urlp_get_hostname_component(
pattern: UrlpPattern,
res: *mut UrlpComponent,
) {
let q_pattern = &*(pattern.0 as *const Uq::UrlPattern);
let tmp: UrlpComponent = q_pattern.hostname.clone().into();
*res = tmp;
}
#[no_mangle]
pub unsafe extern "C" fn urlp_get_port_component(pattern: UrlpPattern, res: *mut UrlpComponent) {
let q_pattern = &*(pattern.0 as *const Uq::UrlPattern);
let tmp: UrlpComponent = q_pattern.port.clone().into();
*res = tmp;
}
#[no_mangle]
pub unsafe extern "C" fn urlp_get_pathname_component(
pattern: UrlpPattern,
res: *mut UrlpComponent,
) {
let q_pattern = &*(pattern.0 as *const Uq::UrlPattern);
let tmp: UrlpComponent = q_pattern.pathname.clone().into();
*res = tmp;
}
#[no_mangle]
pub unsafe extern "C" fn urlp_get_search_component(pattern: UrlpPattern, res: *mut UrlpComponent) {
let q_pattern = &*(pattern.0 as *const Uq::UrlPattern);
let tmp: UrlpComponent = q_pattern.search.clone().into();
*res = tmp;
}
#[no_mangle]
pub unsafe extern "C" fn urlp_get_hash_component(pattern: UrlpPattern, res: *mut UrlpComponent) {
let q_pattern = &*(pattern.0 as *const Uq::UrlPattern);
let tmp: UrlpComponent = q_pattern.hash.clone().into();
*res = tmp;
}
#[no_mangle]
pub unsafe extern "C" fn urlp_get_has_regexp_groups(pattern: UrlpPattern) -> bool {
let q_pattern = &*(pattern.0 as *const Uq::UrlPattern);
q_pattern.has_regexp_groups
}
// note: the ThinVec<MaybeString> is being returned as an out-param
// because if you attempt to return the vector in the normal way
// we end up with an incongruent ABI layout between C++ and rust
// which re-orders the input parameter pointers such that we cannot reference them
// by address reliably.
// Ie. ThinVec/nsTArray is a non-trivial for the purpose of calls
// so we use an out-param instead. We see similar patterns elsewhere in this file
// for return values on the C++/rust ffi boundary
#[no_mangle]
pub extern "C" fn urlp_matcher_matches_component(
matcher: &UrlpMatcher,
input: &nsACString,
ignore_case: bool,
res: &mut ThinVec<MaybeString>,
) -> bool {
debug!("urlp_matcher_matches_component()");
let q_matcher: Uq::Matcher = matcher.clone().into();
let i: &str = &input.to_string();
let matches = matcher_matches(&q_matcher, i, ignore_case);
if let Some(inner_vec) = matches {
for item in inner_vec {
match item {
Some(s) => {
res.push(MaybeString {
string: s.into(),
valid: true,
});
}
None => {
res.push(MaybeString {
string: nsCString::from(""),
valid: false,
});
}
}
}
true
} else {
false
}
}
// note: can't return Result<Option<...>> since cbindgen doesn't handle well
// so we need to return a type that can be used in C++ and rust
#[no_mangle]
pub extern "C" fn urlp_process_match_input_from_string(
url_str: *const nsACString,
base_url: *const nsACString,
res: *mut UrlpMatchInputAndInputs,
) -> bool {
debug!("urlp_process_match_input_from_string()");
if let Some(url) = unsafe { url_str.as_ref().map(|x| x.to_utf8().into_owned()) } {
let str_or_init = Uq::StringOrInit::String(url);
let maybe_base_url = if base_url.is_null() {
None
} else {
let x = unsafe { (*base_url).as_str_unchecked() };
Some(x)
};
let match_input_and_inputs = Uq::process_match_input(str_or_init, maybe_base_url);
if let Ok(Some(tuple_struct)) = match_input_and_inputs {
// parse "input"
let match_input = tuple_struct.0;
let maybe_match_input = Uq::parse_match_input(match_input);
if maybe_match_input.is_none() {
return false;
}
// convert "inputs"
let tuple_soi_and_string = tuple_struct.1;
let string = match tuple_soi_and_string.0 {
Uq::StringOrInit::String(x) => x,
_ => {
assert!(
false,
"Pulling init out of StringOrInit shouldn't happen in _from_string"
);
return false;
}
};
let base = match tuple_soi_and_string.1 {
Some(x) => MaybeString::new(&nsCString::from(x)),
_ => MaybeString::none(),
};
let tmp = UrlpMatchInputAndInputs {
input: maybe_match_input.unwrap().into(),
inputs: UrlpInput {
string_or_init_type: UrlpStringOrInitType::String,
str: nsCString::from(string),
init: UrlpInit::none(),
base,
},
};
unsafe { *res = tmp };
return true;
} else {
return false;
}
}
false
}
#[no_mangle]
pub extern "C" fn urlp_process_match_input_from_init(
init: &UrlpInit,
base_url: *const nsACString,
res: *mut UrlpMatchInputAndInputs,
) -> bool {
debug!("urlp_process_match_input_from_init()");
let q_init = init.into();
let str_or_init = Uq::StringOrInit::Init(q_init);
let maybe_base_url = if base_url.is_null() {
None
} else {
Some(unsafe { (*base_url).as_str_unchecked() })
};
let match_input_and_inputs = Uq::process_match_input(str_or_init, maybe_base_url);
// an empty string passed to base_url will cause url-parsing failure
// in process_match_input, which we handle here
if let Ok(Some(tuple_struct)) = match_input_and_inputs {
let match_input = tuple_struct.0;
let maybe_match_input = Uq::parse_match_input(match_input);
if maybe_match_input.is_none() {
return false;
}
let tuple_soi_and_string = tuple_struct.1;
let init = match tuple_soi_and_string.0 {
Uq::StringOrInit::Init(x) => x,
_ => {
assert!(
false,
"Pulling string out of StringOrInit shouldn't happen in _from_init"
);
return false;
}
};
let base = match tuple_soi_and_string.1 {
Some(x) => MaybeString::new(&nsCString::from(x)),
_ => MaybeString::none(),
};
let tmp = UrlpMatchInputAndInputs {
input: maybe_match_input.unwrap().into(),
inputs: UrlpInput {
string_or_init_type: UrlpStringOrInitType::Init,
str: nsCString::new(),
init: init.into(),
base,
},
};
unsafe { *res = tmp };
return true;
} else {
return false;
}
}
|