From 5216332c72af252de681c72810ab2e677c9762bd Mon Sep 17 00:00:00 2001 From: benshi <807629978@qq.com> Date: Mon, 8 Jul 2024 10:01:59 +0800 Subject: [PATCH] add http hook for get all headers fn --- Cargo.toml | 2 +- examples/server.rs | 15 ++++++++++++-- src/obj.rs | 50 +++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 61 insertions(+), 6 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4362e28..609872c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ workspace = { members = ["rszlm-sys"] } [package] name = "rszlm" -version = "0.1.7" +version = "0.1.9" edition = "2021" authors = ["shiben. "] description = "ZLMediaKit rust api" diff --git a/examples/server.rs b/examples/server.rs index 96507f9..13b633b 100644 --- a/examples/server.rs +++ b/examples/server.rs @@ -1,4 +1,5 @@ use std::collections::HashMap; +use std::str::FromStr; use axum::response::IntoResponse; use axum::{body::Body, routing::get, Router}; @@ -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) diff --git a/src/obj.rs b/src/obj.rs index 50c7f1a..ee24e92 100644 --- a/src/obj.rs +++ b/src/obj.rs @@ -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); @@ -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 { + 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; +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 for Parser {