@@ -4,7 +4,7 @@ use async_trait::async_trait;
4
4
use ctor:: ctor;
5
5
use rand:: seq:: SliceRandom ;
6
6
use reqwest:: {
7
- header:: { HeaderMap , HeaderName , HeaderValue } ,
7
+ header:: { HeaderMap , HeaderName , HeaderValue , CONTENT_TYPE , COOKIE , USER_AGENT } ,
8
8
multipart, redirect, Client , Method , RequestBuilder , Response ,
9
9
} ;
10
10
use url:: Url ;
@@ -150,12 +150,12 @@ impl HTTP {
150
150
& self ,
151
151
creds : & Credentials ,
152
152
csrf : Option < csrf:: Token > ,
153
- mut request : RequestBuilder ,
153
+ mut builder : RequestBuilder ,
154
154
) -> RequestBuilder {
155
155
let mut do_body = true ;
156
156
if self . strategy == Strategy :: BasicAuth {
157
157
// set basic authentication data
158
- request = request . basic_auth ( & creds. username , Some ( & creds. password ) ) ;
158
+ builder = builder . basic_auth ( & creds. username , Some ( & creds. password ) ) ;
159
159
} else if self . strategy == Strategy :: Form {
160
160
// set form data
161
161
let fields = payload:: parse_fields ( self . payload . as_ref ( ) , creds) . unwrap ( ) ;
@@ -170,7 +170,7 @@ impl HTTP {
170
170
form = form. text ( token. name . clone ( ) , token. value . clone ( ) ) ;
171
171
}
172
172
173
- request = request . multipart ( form) ;
173
+ builder = builder . multipart ( form) ;
174
174
175
175
// we already added the --http-body value as fields
176
176
do_body = false ;
@@ -179,35 +179,41 @@ impl HTTP {
179
179
// do we have any fields left to add?
180
180
if do_body && self . payload . is_some ( ) {
181
181
if method_requires_payload ( & self . method ) {
182
- // add as body
183
- let mut body = payload:: parse_body ( self . payload . as_ref ( ) , creds) . unwrap ( ) ;
182
+ // check if we have to urlencode fields
183
+ if self . headers . get ( CONTENT_TYPE ) . unwrap ( ) == "application/x-www-form-urlencoded" {
184
+ let mut form_fields =
185
+ payload:: parse_fields ( self . payload . as_ref ( ) , creds) . unwrap ( ) ;
184
186
185
- // handle csrf
186
- if let Some ( token) = csrf. as_ref ( ) {
187
- body . push_str ( & format ! ( "&{}={}" , token. name, token. value) ) ;
188
- }
187
+ // handle csrf
188
+ if let Some ( token) = csrf. as_ref ( ) {
189
+ form_fields . push ( ( token. name . to_owned ( ) , token. value . to_owned ( ) ) ) ;
190
+ }
189
191
190
- // log::info!("http.body={}", &body);
191
- request = request. body ( body) ;
192
- // check if Content-Type is set already, if not set default (tnx to @zip609)
193
- if !self . headers . contains_key ( "Content-Type" ) {
194
- request = request. header ( "Content-Type" , "application/x-www-form-urlencoded" ) ;
192
+ builder = builder. form ( & form_fields) ;
193
+ } else {
194
+ // add as raw body
195
+ let mut body = payload:: parse_body ( self . payload . as_ref ( ) , creds) . unwrap ( ) ;
196
+ // handle csrf
197
+ if let Some ( token) = csrf. as_ref ( ) {
198
+ body. push_str ( & format ! ( "&{}={}" , token. name, token. value) ) ;
199
+ }
200
+ builder = builder. body ( body) ;
195
201
}
196
202
} else {
197
203
// add as query string
198
- let mut query = payload:: parse_fields ( self . payload . as_ref ( ) , creds) . unwrap ( ) ;
204
+ let mut query_fields = payload:: parse_fields ( self . payload . as_ref ( ) , creds) . unwrap ( ) ;
199
205
200
206
// handle csrf
201
207
if let Some ( token) = csrf. as_ref ( ) {
202
- query . push ( ( token. name . clone ( ) , token. value . clone ( ) ) ) ;
208
+ query_fields . push ( ( token. name . clone ( ) , token. value . clone ( ) ) ) ;
203
209
}
204
210
205
211
// log::info!("http.query={:?}", &query);
206
- request = request . query ( & query ) ;
212
+ builder = builder . query ( & query_fields ) ;
207
213
}
208
214
}
209
215
210
- request
216
+ builder
211
217
}
212
218
213
219
async fn is_success ( & self , response : Response ) -> Option < Success > {
@@ -216,7 +222,7 @@ impl HTTP {
216
222
return None ;
217
223
}
218
224
219
- let content_type = if let Some ( ctype) = response. headers ( ) . get ( "content-type" ) {
225
+ let content_type = if let Some ( ctype) = response. headers ( ) . get ( CONTENT_TYPE ) {
220
226
ctype
221
227
. to_str ( )
222
228
. unwrap ( )
@@ -259,7 +265,7 @@ impl HTTP {
259
265
260
266
if self . random_ua {
261
267
headers. append (
262
- "User-Agent" ,
268
+ USER_AGENT ,
263
269
HeaderValue :: from_str ( ua:: USER_AGENTS . choose ( & mut rand:: thread_rng ( ) ) . unwrap ( ) )
264
270
. unwrap ( ) ,
265
271
) ;
@@ -305,7 +311,7 @@ impl HTTP {
305
311
if let Some ( token) = token. as_ref ( ) {
306
312
// set session cookie for CSRF
307
313
if !token. cookie . is_empty ( ) {
308
- headers. append ( "Cookie" , HeaderValue :: from_str ( & token. cookie ) . unwrap ( ) ) ;
314
+ headers. append ( COOKIE , HeaderValue :: from_str ( & token. cookie ) . unwrap ( ) ) ;
309
315
}
310
316
}
311
317
@@ -323,12 +329,11 @@ impl HTTP {
323
329
324
330
// setup body
325
331
request = self . setup_request_body ( creds, csrf_token, request) ;
326
-
327
332
// execute
328
333
match request. send ( ) . await {
329
334
Err ( e) => Err ( e. to_string ( ) ) ,
330
335
Ok ( res) => {
331
- let cookie = if let Some ( cookie) = res. headers ( ) . get ( "cookie" ) {
336
+ let cookie = if let Some ( cookie) = res. headers ( ) . get ( COOKIE ) {
332
337
cookie. to_str ( ) . unwrap ( ) . to_owned ( )
333
338
} else {
334
339
"" . to_owned ( )
@@ -447,6 +452,14 @@ impl Plugin for HTTP {
447
452
) ;
448
453
}
449
454
455
+ // check if Content-Type is set already, if not set default (tnx to @zip609)
456
+ if !self . headers . contains_key ( "Content-Type" ) {
457
+ self . headers . insert (
458
+ CONTENT_TYPE ,
459
+ HeaderValue :: from_static ( "application/x-www-form-urlencoded" ) ,
460
+ ) ;
461
+ }
462
+
450
463
// check that a payload was provided if needed
451
464
if method_requires_payload ( & self . method ) && opts. http . http_payload . is_none ( ) {
452
465
return Err ( format ! (
0 commit comments