1
- use isahc:: {
2
- config:: { RedirectPolicy , SslOption } ,
3
- prelude:: * ,
4
- HttpClientBuilder , Request ,
5
- } ;
6
1
use mlua:: UserData ;
2
+ use reqwest:: header:: { HeaderMap , HeaderName , HeaderValue } ;
3
+ use reqwest:: { redirect, Client , Proxy } ;
7
4
use std:: { collections:: HashMap , time:: Duration } ;
8
5
use tealr:: { mlu:: FromToLua , TypeName } ;
9
6
@@ -26,19 +23,17 @@ pub enum RespType {
26
23
27
24
impl UserData for Sender {
28
25
fn add_methods < ' lua , M : mlua:: UserDataMethods < ' lua , Self > > ( methods : & mut M ) {
29
- methods. add_method_mut (
30
- "set_proxy" , |_, this, the_proxy : mlua:: Value | {
31
- match the_proxy {
32
- mlua:: Value :: String ( new_proxy) => {
33
- this. proxy = Some ( new_proxy. to_str ( ) . unwrap ( ) . to_string ( ) ) ;
34
- } ,
35
- _ => {
36
- this. proxy = None ;
37
- }
38
- } ;
39
- Ok ( ( ) )
40
- }
41
- ) ;
26
+ methods. add_method_mut ( "set_proxy" , |_, this, the_proxy : mlua:: Value | {
27
+ match the_proxy {
28
+ mlua:: Value :: String ( new_proxy) => {
29
+ this. proxy = Some ( new_proxy. to_str ( ) . unwrap ( ) . to_string ( ) ) ;
30
+ }
31
+ _ => {
32
+ this. proxy = None ;
33
+ }
34
+ } ;
35
+ Ok ( ( ) )
36
+ } ) ;
42
37
methods. add_method_mut ( "set_timeout" , |_, this, timeout : u64 | {
43
38
this. timeout = timeout;
44
39
Ok ( ( ) )
@@ -50,22 +45,28 @@ impl UserData for Sender {
50
45
} ) ;
51
46
methods. add_async_method (
52
47
"send" ,
53
- |_, this, ( method, url, req_body) : ( String , String , mlua:: Value ) | async move {
48
+ |_, this, ( method, url, req_body, req_headers ) : ( String , String , mlua :: Value , mlua:: Value ) | async move {
54
49
let body: String ;
55
- if req_body. type_name ( ) == "string" {
56
- body = match req_body {
57
- mlua:: Value :: String ( body) => {
58
- body. to_str ( ) . unwrap ( ) . to_string ( )
59
- } ,
60
- _ => {
61
- "" . to_string ( )
62
- }
63
- } ;
64
- } else {
65
- body = "" . to_string ( ) ;
66
- }
50
+ let mut all_headers = HeaderMap :: new ( ) ;
51
+ match req_headers {
52
+ mlua:: Value :: Table ( current_headers) => {
53
+ current_headers. pairs :: < String , String > ( ) . for_each ( |currentheader| {
54
+ all_headers. insert ( HeaderName :: from_bytes ( currentheader. clone ( ) . unwrap ( ) . 0 . as_bytes ( ) ) . unwrap ( ) , HeaderValue :: from_bytes ( currentheader. unwrap ( ) . 1 . as_bytes ( ) ) . unwrap ( ) ) ;
55
+ } ) ;
56
+ } ,
57
+ _ => {
58
+ }
59
+ } ;
60
+ body = match req_body {
61
+ mlua:: Value :: String ( body) => {
62
+ body. to_str ( ) . unwrap ( ) . to_string ( )
63
+ } ,
64
+ _ => {
65
+ "" . to_string ( )
66
+ }
67
+ } ;
67
68
68
- let resp = this. send ( & method, url, body) . await ;
69
+ let resp = this. send ( & method, url, body, all_headers ) . await ;
69
70
Ok ( resp)
70
71
} ,
71
72
) ;
@@ -81,41 +82,55 @@ impl Sender {
81
82
}
82
83
}
83
84
84
- fn build_client ( & self ) -> Result < isahc:: HttpClient , isahc:: Error > {
85
- HttpClientBuilder :: new ( )
86
- . timeout ( Duration :: from_secs ( self . timeout ) )
87
- . redirect_policy ( RedirectPolicy :: Limit ( self . redirects ) )
88
- . proxy ( {
89
- match & self . proxy {
90
- Some ( proxy) => {
91
- Some ( proxy. parse ( ) . unwrap ( ) )
92
- } ,
93
- None => {
94
- None
95
- }
96
- }
97
- } )
98
- . ssl_options ( SslOption :: DANGER_ACCEPT_INVALID_CERTS )
99
- . ssl_options ( SslOption :: DANGER_ACCEPT_REVOKED_CERTS )
100
- . ssl_options ( SslOption :: DANGER_ACCEPT_INVALID_HOSTS )
101
- . build ( )
85
+ fn build_client ( & self ) -> Result < reqwest:: Client , reqwest:: Error > {
86
+ match & self . proxy {
87
+ Some ( the_proxy) => {
88
+ Client :: builder ( )
89
+ . timeout ( Duration :: from_secs ( self . timeout ) )
90
+ . redirect ( redirect:: Policy :: limited ( self . redirects as usize ) )
91
+ . proxy ( Proxy :: all ( the_proxy) . unwrap ( ) )
92
+ . no_trust_dns ( )
93
+ . user_agent (
94
+ "Mozilla/5.0 (X11; Manjaro; Linux x86_64; rv:100.0) Gecko/20100101 Firefox/100.0" ,
95
+ )
96
+ . danger_accept_invalid_certs ( true )
97
+ . build ( )
98
+ }
99
+ None => {
100
+ Client :: builder ( )
101
+ . timeout ( Duration :: from_secs ( self . timeout ) )
102
+ . redirect ( redirect:: Policy :: limited ( self . redirects as usize ) )
103
+ . no_proxy ( )
104
+ . no_trust_dns ( )
105
+ . user_agent (
106
+ "Mozilla/5.0 (X11; Manjaro; Linux x86_64; rv:100.0) Gecko/20100101 Firefox/100.0" ,
107
+ )
108
+ . danger_accept_invalid_certs ( true )
109
+ . build ( )
110
+ }
111
+ }
102
112
}
103
- pub async fn send ( & self , method : & str , url : String , body : String ) -> HashMap < String , RespType > {
113
+ pub async fn send (
114
+ & self ,
115
+ method : & str ,
116
+ url : String ,
117
+ body : String ,
118
+ headers : HeaderMap ,
119
+ ) -> HashMap < String , RespType > {
104
120
let mut resp_data: HashMap < String , RespType > = HashMap :: new ( ) ;
105
- let current_request = Request :: builder ( )
106
- . uri ( url)
107
- . method ( method)
108
- . header ( "User-agent" , "Mozilla/5.0 (X11; Manjaro; Linux x86_64; rv:100.0) Gecko/20100101 Firefox/100.0" )
109
- . body ( body)
110
- . unwrap ( ) ;
111
- let req = self
121
+ match self
112
122
. build_client ( )
113
123
. unwrap ( )
114
- . send_async ( current_request)
115
- . await ;
116
-
117
- match req {
118
- Ok ( mut resp) => {
124
+ . request (
125
+ reqwest:: Method :: from_bytes ( method. as_bytes ( ) ) . unwrap ( ) ,
126
+ url. clone ( ) ,
127
+ )
128
+ . headers ( headers)
129
+ . body ( body)
130
+ . send ( )
131
+ . await
132
+ {
133
+ Ok ( resp) => {
119
134
let mut resp_headers: HashMap < String , String > = HashMap :: new ( ) ;
120
135
resp. headers ( )
121
136
. iter ( )
@@ -125,10 +140,7 @@ impl Sender {
125
140
header_value. to_str ( ) . unwrap ( ) . to_string ( ) ,
126
141
) ;
127
142
} ) ;
128
- resp_data. insert (
129
- "url" . to_string ( ) ,
130
- RespType :: Str ( resp. effective_uri ( ) . unwrap ( ) . to_string ( ) ) ,
131
- ) ;
143
+ resp_data. insert ( "url" . to_string ( ) , RespType :: Str ( url) ) ;
132
144
resp_data. insert (
133
145
"status" . to_string ( ) ,
134
146
RespType :: Str ( resp. status ( ) . to_string ( ) ) ,
0 commit comments