Skip to content

Commit be59928

Browse files
committed
fix: handle origin property
1 parent 38e0350 commit be59928

File tree

1 file changed

+6
-13
lines changed

1 file changed

+6
-13
lines changed

src/lib.rs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use crate::ffi::{ada_parse, ada_parse_with_base};
21
use thiserror::Error;
32

43
pub mod ffi {
@@ -73,7 +72,7 @@ pub mod ffi {
7372
pub fn ada_get_url_components(url: *mut ada_url) -> ada_url_components;
7473

7574
// Getters
76-
pub fn ada_get_origin(url: *mut ada_url) -> *mut ada_owned_string;
75+
pub fn ada_get_origin(url: *mut ada_url) -> ada_owned_string;
7776
pub fn ada_get_href(url: *mut ada_url) -> ada_string;
7877
pub fn ada_get_username(url: *mut ada_url) -> ada_string;
7978
pub fn ada_get_password(url: *mut ada_url) -> ada_string;
@@ -117,17 +116,11 @@ pub enum Error {
117116
}
118117

119118
pub struct Url {
120-
origin: Option<*mut ffi::ada_owned_string>,
121119
url: *mut ffi::ada_url,
122120
}
123121

124122
impl Drop for Url {
125123
fn drop(&mut self) {
126-
if let Some(origin) = self.origin {
127-
unsafe {
128-
ffi::ada_free_owned_string(origin);
129-
}
130-
}
131124
unsafe {
132125
ffi::ada_free(self.url);
133126
}
@@ -146,19 +139,18 @@ impl Url {
146139
pub fn parse(input: &str, base: Option<&str>) -> Result<Url, Error> {
147140
let url_aggregator = match base {
148141
Some(base) => unsafe {
149-
ada_parse_with_base(
142+
ffi::ada_parse_with_base(
150143
input.as_ptr().cast(),
151144
input.len(),
152145
base.as_ptr().cast(),
153146
base.len(),
154147
)
155148
},
156-
None => unsafe { ada_parse(input.as_ptr().cast(), input.len()) },
149+
None => unsafe { ffi::ada_parse(input.as_ptr().cast(), input.len()) },
157150
};
158151

159152
if unsafe { ffi::ada_is_valid(url_aggregator) } {
160153
Ok(Url {
161-
origin: None,
162154
url: url_aggregator,
163155
})
164156
} else {
@@ -190,8 +182,9 @@ impl Url {
190182

191183
pub fn origin(&mut self) -> &str {
192184
unsafe {
193-
self.origin = Some(ffi::ada_get_origin(self.url));
194-
self.origin.map(|o| (*o).as_ref()).unwrap_or("")
185+
let out = ffi::ada_get_origin(self.url);
186+
let slice = std::slice::from_raw_parts(out.data.cast(), out.length);
187+
std::str::from_utf8_unchecked(slice)
195188
}
196189
}
197190

0 commit comments

Comments
 (0)