Skip to content

Commit

Permalink
add http headers function
Browse files Browse the repository at this point in the history
  • Loading branch information
BenLocal committed Aug 6, 2024
1 parent 4b64e20 commit bda9a02
Showing 1 changed file with 44 additions and 3 deletions.
47 changes: 44 additions & 3 deletions src/obj.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
use std::{os::raw::c_char, ptr};
use std::{
cell::RefCell,
collections::HashMap,
os::raw::c_char,
ptr::{self, null_mut},
};

use rszlm_sys::*;

use crate::{const_ptr_to_string, const_str_to_ptr};
use crate::{box_to_mut_void_ptr, const_ptr_to_string, const_str_to_ptr};

#[derive(Debug)]
pub struct SockInfo(mk_sock_info);
Expand Down Expand Up @@ -296,7 +301,43 @@ impl Parser {
}

pub fn body(&self) -> String {
unsafe { const_ptr_to_string!(mk_parser_get_tail(self.0)) }
unsafe { const_ptr_to_string!(mk_parser_get_content(self.0, null_mut() as *mut _)) }
}

pub fn headers(&self) -> HashMap<String, String> {
let headers = std::rc::Rc::new(RefCell::new(HashMap::new()));

let headers_clone = headers.clone();
self.headers_for_each(Box::new(move |key, val| {
headers_clone.borrow_mut().insert(key, val);
}));

let tmp = headers.as_ref().borrow().to_owned();
tmp
}

fn headers_for_each(&self, cb: ParserHeadersForEachCallbackFn) {
unsafe {
mk_parser_headers_for_each(
self.0,
Some(parser_headers_for_each),
box_to_mut_void_ptr!(cb),
)
}
}
}

type ParserHeadersForEachCallbackFn = Box<dyn FnMut(String, String) + 'static>;
extern "C" fn parser_headers_for_each(
user_data: *mut ::std::os::raw::c_void,
key: *const ::std::os::raw::c_char,
val: *const ::std::os::raw::c_char,
) {
unsafe {
let cb: &mut ParserHeadersForEachCallbackFn = std::mem::transmute(user_data);
let key = const_ptr_to_string!(key);
let val = const_ptr_to_string!(val);
cb(key, val);
}
}

Expand Down

0 comments on commit bda9a02

Please sign in to comment.