Skip to content

Commit

Permalink
add http hook for get all headers fn
Browse files Browse the repository at this point in the history
  • Loading branch information
BenLocal committed Jul 8, 2024
1 parent 348c83a commit 5216332
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ workspace = { members = ["rszlm-sys"] }

[package]
name = "rszlm"
version = "0.1.7"
version = "0.1.9"
edition = "2021"
authors = ["shiben. <benshi0v0@gmail.com>"]
description = "ZLMediaKit rust api"
Expand Down
15 changes: 13 additions & 2 deletions examples/server.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::collections::HashMap;
use std::str::FromStr;

use axum::response::IntoResponse;
use axum::{body::Body, routing::get, Router};
Expand Down Expand Up @@ -269,12 +270,22 @@ fn start_zlm_background(
};

let uri = format!("http://127.0.0.1:{}{}", AXUM_PORT, path_query);
if let Ok(req) = hyper::Request::builder()
let headers = msg.parser.headers();

if let Ok(mut req) = hyper::Request::builder()
.method(msg.parser.method().as_str())
.uri(uri)
.body(Body::from(msg.parser.body()))
{
// TODO copy request headers
if !headers.is_empty() {
for (k, v) in headers {
req.headers_mut().insert(
hyper::http::HeaderName::from_str(&k).unwrap(),
hyper::http::HeaderValue::from_str(&v).unwrap(),
);
}
}

let resp = runtime.block_on(async move {
CLIENT
.request(req)
Expand Down
50 changes: 47 additions & 3 deletions src/obj.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
use std::ptr;
use std::{
cell::RefCell,
collections::HashMap,
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 @@ -295,9 +299,49 @@ impl Parser {
unsafe { const_ptr_to_string!(mk_parser_get_header(self.0, key.as_ptr())) }
}

pub fn body(&self) -> String {
pub fn protocol(&self) -> String {
unsafe { const_ptr_to_string!(mk_parser_get_tail(self.0)) }
}

pub fn body(&self) -> String {
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);
}
}

impl From<mk_parser> for Parser {
Expand Down

0 comments on commit 5216332

Please sign in to comment.