pub use http::*;

extern "C" {
    fn __k3_http_get(url_ptr: u32) -> u32;
}

pub fn get(url: &str) -> Option<Vec<u8>> {
    let mut url_buf = (url.len() as u32).to_le_bytes().to_vec();
    url_buf.extend_from_slice(url.as_bytes());
    unsafe {
        let res_ptr = __k3_http_get(url_buf.as_ptr() as u32) as *mut u8;
        if res_ptr == std::ptr::null_mut() {
            None
        } else {
            let len =
                u32::from_le_bytes(std::slice::from_raw_parts(res_ptr, 4).try_into().unwrap());
            let mut res = vec![0u8; len as usize];
            res.copy_from_slice(std::slice::from_raw_parts(res_ptr.add(4), len as usize));
            Some(res)
        }
    }
}
