@@ -127,6 +127,7 @@ macro_rules! implement_http_transport {
127
127
$( #[ $attr: meta] ) *
128
128
pub struct $typename: ident;
129
129
fn spawn( $( $argname: ident: $argty: ty, ) * ) $body: block
130
+ fn http_client( $hc_options: ident: & ClientOptions , $hc_client: ident: Option <$hc_client_ty: ty>) -> $hc_ret: ty $hc_body: block
130
131
) => {
131
132
$( #[ $attr] ) *
132
133
pub struct $typename {
@@ -139,20 +140,34 @@ macro_rules! implement_http_transport {
139
140
140
141
impl $typename {
141
142
/// Creates a new transport.
142
- pub fn new( options: & ClientOptions ) -> $typename {
143
+ pub fn new( options: & ClientOptions ) -> Self {
144
+ Self :: new_internal( options, None )
145
+ }
146
+
147
+ /// Creates a new transport that uses the passed HTTP client.
148
+ pub fn with_client( options: & ClientOptions , $hc_client: $hc_client_ty) -> Self {
149
+ Self :: new_internal( options, Some ( $hc_client) )
150
+ }
151
+
152
+ /// Creates a new transport that uses the passed HTTP client or builds a new one.
153
+ fn new_internal( options: & ClientOptions , $hc_client: Option <$hc_client_ty>) -> Self {
143
154
fn spawn( $( $argname: $argty, ) * ) -> JoinHandle <( ) > { $body }
144
155
156
+ fn http_client( $hc_options: & ClientOptions , $hc_client: Option <$hc_client_ty>) -> $hc_ret { $hc_body }
157
+
145
158
let ( sender, receiver) = sync_channel( 30 ) ;
146
159
let shutdown_signal = Arc :: new( Condvar :: new( ) ) ;
147
160
let shutdown_immediately = Arc :: new( AtomicBool :: new( false ) ) ;
148
161
#[ allow( clippy:: mutex_atomic) ]
149
162
let queue_size = Arc :: new( Mutex :: new( 0 ) ) ;
163
+ let http_client = http_client( options, $hc_client) ;
150
164
let _handle = Some ( spawn(
151
165
options,
152
166
receiver,
153
167
shutdown_signal. clone( ) ,
154
168
shutdown_immediately. clone( ) ,
155
169
queue_size. clone( ) ,
170
+ http_client,
156
171
) ) ;
157
172
$typename {
158
173
sender: Mutex :: new( sender) ,
@@ -216,19 +231,10 @@ implement_http_transport! {
216
231
signal: Arc <Condvar >,
217
232
shutdown_immediately: Arc <AtomicBool >,
218
233
queue_size: Arc <Mutex <usize >>,
234
+ http_client: Client ,
219
235
) {
220
236
let dsn = options. dsn. clone( ) . unwrap( ) ;
221
237
let user_agent = options. user_agent. to_string( ) ;
222
- let http_proxy = options. http_proxy. as_ref( ) . map( |x| x. to_string( ) ) ;
223
- let https_proxy = options. https_proxy. as_ref( ) . map( |x| x. to_string( ) ) ;
224
- let mut client = Client :: builder( ) ;
225
- if let Some ( url) = http_proxy {
226
- client = client. proxy( Proxy :: http( & url) . unwrap( ) ) ;
227
- } ;
228
- if let Some ( url) = https_proxy {
229
- client = client. proxy( Proxy :: https( & url) . unwrap( ) ) ;
230
- } ;
231
- let client = client. build( ) . unwrap( ) ;
232
238
233
239
let mut disabled = None :: <SystemTime >;
234
240
@@ -260,7 +266,7 @@ implement_http_transport! {
260
266
}
261
267
}
262
268
263
- match client
269
+ match http_client
264
270
. post( url. as_str( ) )
265
271
. json( & event)
266
272
. header( "X-Sentry-Auth" , dsn. to_auth( Some ( & user_agent) ) . to_string( ) )
@@ -291,6 +297,21 @@ implement_http_transport! {
291
297
}
292
298
} ) . unwrap( )
293
299
}
300
+
301
+ fn http_client( options: & ClientOptions , client: Option <Client >) -> Client {
302
+ client. unwrap_or_else( || {
303
+ let http_proxy = options. http_proxy. as_ref( ) . map( |x| x. to_string( ) ) ;
304
+ let https_proxy = options. https_proxy. as_ref( ) . map( |x| x. to_string( ) ) ;
305
+ let mut client = Client :: builder( ) ;
306
+ if let Some ( url) = http_proxy {
307
+ client = client. proxy( Proxy :: http( & url) . unwrap( ) ) ;
308
+ } ;
309
+ if let Some ( url) = https_proxy {
310
+ client = client. proxy( Proxy :: https( & url) . unwrap( ) ) ;
311
+ } ;
312
+ client. build( ) . unwrap( )
313
+ } )
314
+ }
294
315
}
295
316
296
317
#[ cfg( feature = "with_curl_transport" ) ]
@@ -306,14 +327,15 @@ implement_http_transport! {
306
327
signal: Arc <Condvar >,
307
328
shutdown_immediately: Arc <AtomicBool >,
308
329
queue_size: Arc <Mutex <usize >>,
330
+ http_client: curl:: easy:: Easy ,
309
331
) {
310
332
let dsn = options. dsn. clone( ) . unwrap( ) ;
311
333
let user_agent = options. user_agent. to_string( ) ;
312
334
let http_proxy = options. http_proxy. as_ref( ) . map( |x| x. to_string( ) ) ;
313
335
let https_proxy = options. https_proxy. as_ref( ) . map( |x| x. to_string( ) ) ;
314
336
315
337
let mut disabled = None :: <SystemTime >;
316
- let mut handle = curl :: easy :: Easy :: new ( ) ;
338
+ let mut handle = http_client ;
317
339
318
340
thread:: spawn( move || {
319
341
sentry_debug!( "spawning curl transport" ) ;
@@ -417,6 +439,10 @@ implement_http_transport! {
417
439
}
418
440
} )
419
441
}
442
+
443
+ fn http_client( _options: & ClientOptions , client: Option <curl:: easy:: Easy >) -> curl:: easy:: Easy {
444
+ client. unwrap_or_else( curl:: easy:: Easy :: new)
445
+ }
420
446
}
421
447
422
448
#[ cfg( feature = "with_reqwest_transport" ) ]
0 commit comments