File: lib.rs

package info (click to toggle)
nodejs 22.14.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 246,928 kB
  • sloc: cpp: 1,582,349; javascript: 582,017; ansic: 82,400; python: 60,561; sh: 4,009; makefile: 2,263; asm: 1,732; pascal: 1,565; perl: 248; lisp: 222; xml: 42
file content (44 lines) | stat: -rw-r--r-- 1,414 bytes parent folder | download | duplicates (6)
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
use url::Url;
use std::slice;
use libc::{c_char, size_t};

extern crate url;
extern crate libc;

#[no_mangle]
pub unsafe extern "C" fn parse_url(raw_input: *const c_char, raw_input_length: size_t) -> *mut Url {
  let input = std::str::from_utf8_unchecked(slice::from_raw_parts(raw_input as *const u8, raw_input_length));
  // This code would assume that the URL is parsed successfully:
  // let result = Url::parse(input).unwrap();
  // Box::into_raw(Box::new(result))
  // But we might get an invalid input. So we want to return null in case of
  // error. We can do it in such a manner:
  match Url::parse(input) {
    Ok(result) => Box::into_raw(Box::new(result)),
    Err(_) => std::ptr::null_mut(),
  }
}

#[no_mangle]
pub unsafe extern "C" fn parse_url_to_href(raw_input: *const c_char, raw_input_length: size_t) -> *const c_char {
  let input = std::str::from_utf8_unchecked(slice::from_raw_parts(raw_input as *const u8, raw_input_length));
  match Url::parse(input) {
    Ok(result) => std::ffi::CString::new(result.as_str()).unwrap().into_raw(),
    Err(_) => std::ptr::null_mut(),
  }
}

#[no_mangle]
pub unsafe extern "C" fn free_url(raw: *mut Url) {
  if raw.is_null() {
    return;
  }

  drop(Box::from_raw(raw))
}

#[no_mangle]
pub unsafe extern fn free_string(ptr: *const c_char) {
    // Take the ownership back to rust and drop the owner
    let _ = std::ffi::CString::from_raw(ptr as *mut _);
}