1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
#![allow(unused_imports)]

use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::mpsc::{sync_channel, Receiver, SyncSender};
use std::sync::{Arc, Condvar, Mutex};
use std::thread::{self, JoinHandle};
use std::time::{Duration, SystemTime};

#[cfg(any(feature = "reqwest", feature = "curl", feature = "surf"))]
use httpdate::parse_http_date;

#[cfg(feature = "reqwest")]
use reqwest_::{blocking::Client as ReqwestClient, header::RETRY_AFTER, Proxy};

#[cfg(feature = "curl")]
use crate::types::Scheme;
#[cfg(feature = "curl")]
use curl_ as curl;
#[cfg(feature = "curl")]
use std::io::{Cursor, Read};

#[cfg(feature = "surf")]
use futures::executor;
#[cfg(feature = "surf")]
use http_client::native::NativeClient;
#[cfg(feature = "surf")]
use surf_::Client as SurfClient;

use sentry_core::sentry_debug;

use crate::protocol::Event;
use crate::{ClientOptions, Transport, TransportFactory};

/// Creates the default HTTP transport.
///
/// This is the default value for `transport` on the client options.  It
/// creates a `HttpTransport`.  If no http transport was compiled into the
/// library it will panic on transport creation.
#[derive(Clone)]
pub struct DefaultTransportFactory;

impl TransportFactory for DefaultTransportFactory {
    fn create_transport(&self, options: &ClientOptions) -> Arc<dyn Transport> {
        #[cfg(any(feature = "reqwest", feature = "curl", feature = "surf"))]
        {
            Arc::new(HttpTransport::new(options))
        }
        #[cfg(not(any(feature = "reqwest", feature = "curl", feature = "surf")))]
        {
            let _ = options;
            panic!("sentry crate was compiled without transport")
        }
    }
}

#[cfg(any(feature = "reqwest", feature = "curl", feature = "surf"))]
fn parse_retry_after(s: &str) -> Option<SystemTime> {
    if let Ok(value) = s.parse::<f64>() {
        Some(SystemTime::now() + Duration::from_secs(value.ceil() as u64))
    } else if let Ok(value) = parse_http_date(s) {
        Some(value)
    } else {
        None
    }
}

#[allow(unused)]
macro_rules! implement_http_transport {
    (
        $(#[$attr:meta])*
        pub struct $typename:ident;
        fn spawn($($argname:ident: $argty:ty,)*) $body:block
        fn http_client($hc_options:ident: &ClientOptions, $hc_client:ident: Option<$hc_client_ty:ty>) -> $hc_ret:ty $hc_body:block
    ) => {
        $(#[$attr])*
        pub struct $typename {
            sender: Mutex<SyncSender<Option<Event<'static>>>>,
            shutdown_signal: Arc<Condvar>,
            shutdown_immediately: Arc<AtomicBool>,
            queue_size: Arc<Mutex<usize>>,
            _handle: Option<JoinHandle<()>>,
        }

        impl $typename {
            /// Creates a new transport.
            pub fn new(options: &ClientOptions) -> Self {
                Self::new_internal(options, None)
            }

            /// Creates a new transport that uses the passed HTTP client.
            pub fn with_client(options: &ClientOptions, $hc_client: $hc_client_ty) -> Self {
                Self::new_internal(options, Some($hc_client))
            }

            /// Creates a new transport that uses the passed HTTP client or builds a new one.
            fn new_internal(options: &ClientOptions, $hc_client: Option<$hc_client_ty>) -> Self {
                fn spawn($($argname: $argty,)*) -> JoinHandle<()> { $body }

                fn http_client($hc_options: &ClientOptions, $hc_client: Option<$hc_client_ty>) -> $hc_ret { $hc_body }

                let (sender, receiver) = sync_channel(30);
                let shutdown_signal = Arc::new(Condvar::new());
                let shutdown_immediately = Arc::new(AtomicBool::new(false));
                #[allow(clippy::mutex_atomic)]
                let queue_size = Arc::new(Mutex::new(0));
                let http_client = http_client(options, $hc_client);
                let _handle = Some(spawn(
                    options,
                    receiver,
                    shutdown_signal.clone(),
                    shutdown_immediately.clone(),
                    queue_size.clone(),
                    http_client,
                ));
                $typename {
                    sender: Mutex::new(sender),
                    shutdown_signal,
                    shutdown_immediately,
                    queue_size,
                    _handle,
                }
            }
        }

        impl Transport for $typename {
            fn send_event(&self, event: Event<'static>) {
                // we count up before we put the item on the queue and in case the
                // queue is filled with too many items or we shut down, we decrement
                // the count again as there is nobody that can pick it up.
                *self.queue_size.lock().unwrap() += 1;
                if self.sender.lock().unwrap().try_send(Some(event)).is_err() {
                    *self.queue_size.lock().unwrap() -= 1;
                }
            }

            fn shutdown(&self, timeout: Duration) -> bool {
                sentry_debug!("shutting down http transport");
                let guard = self.queue_size.lock().unwrap();
                if *guard == 0 {
                    true
                } else {
                    if let Ok(sender) = self.sender.lock() {
                        sender.send(None).ok();
                    }
                    self.shutdown_signal.wait_timeout(guard, timeout).is_ok()
                }
            }
        }

        impl Drop for $typename {
            fn drop(&mut self) {
                sentry_debug!("dropping http transport");
                self.shutdown_immediately.store(true, Ordering::SeqCst);
                if let Ok(sender) = self.sender.lock() {
                    sender.send(None).ok();
                }
            }
        }
    }
}

#[cfg(feature = "reqwest")]
implement_http_transport! {
    /// A transport can send events via HTTP to sentry via `reqwest`.
    ///
    /// When the `transport` feature is enabled this will currently
    /// be the default transport.  This is separately enabled by the
    /// `reqwest` flag.
    pub struct ReqwestHttpTransport;

    fn spawn(
        options: &ClientOptions,
        receiver: Receiver<Option<Event<'static>>>,
        signal: Arc<Condvar>,
        shutdown_immediately: Arc<AtomicBool>,
        queue_size: Arc<Mutex<usize>>,
        http_client: Option<ReqwestClient>,
    ) {
        let dsn = options.dsn.clone().unwrap();
        let user_agent = options.user_agent.to_string();

        let mut disabled = None::<SystemTime>;
        let http_proxy = options.http_proxy.as_ref().map(ToString::to_string);
        let https_proxy = options.https_proxy.as_ref().map(ToString::to_string);

        thread::Builder::new()
            .name("sentry-transport".to_string())
            .spawn(move || {
                sentry_debug!("spawning reqwest transport");
                let http_client = http_client.unwrap_or_else(|| {
                    let mut builder = ReqwestClient::builder();
                    if let Some(url) = http_proxy {
                        builder = builder.proxy(Proxy::http(&url).unwrap());
                    };
                    if let Some(url) = https_proxy {
                        builder = builder.proxy(Proxy::https(&url).unwrap());
                    };
                    builder.build().unwrap()
                });

                let url = dsn.store_api_url().to_string();

                while let Some(event) = receiver.recv().unwrap_or(None) {
                    // on drop we want to not continue processing the queue.
                    if shutdown_immediately.load(Ordering::SeqCst) {
                        let mut size = queue_size.lock().unwrap();
                        *size = 0;
                        signal.notify_all();
                        break;
                    }

                    // while we are disabled due to rate limits, skip
                    if let Some(ts) = disabled {
                        if let Ok(time_left) = ts.duration_since(SystemTime::now()) {
                            sentry_debug!(
                                "Skipping event send because we're disabled due to rate limits for {}s",
                                time_left.as_secs()
                            );
                            continue;
                        } else {
                            disabled = None;
                        }
                    }

                    match http_client
                        .post(url.as_str())
                        .json(&event)
                        .header("X-Sentry-Auth", dsn.to_auth(Some(&user_agent)).to_string())
                        .send()
                    {
                        Ok(resp) => {
                            if resp.status() == 429 {
                                if let Some(retry_after) = resp
                                    .headers()
                                    .get(RETRY_AFTER)
                                    .and_then(|x| x.to_str().ok())
                                    .and_then(parse_retry_after)
                                {
                                    disabled = Some(retry_after);
                                }
                            }
                        }
                        Err(err) => {
                            sentry_debug!("Failed to send event: {}", err);
                        }
                    }

                    let mut size = queue_size.lock().unwrap();
                    *size -= 1;
                    if *size == 0 {
                        signal.notify_all();
                    }
                }
            }).unwrap()
    }

    fn http_client(
        _options: &ClientOptions,
        client: Option<ReqwestClient>
    ) -> Option<ReqwestClient> {
        client
    }
}

#[cfg(feature = "curl")]
implement_http_transport! {
    /// A transport can send events via HTTP to sentry via `curl`.
    ///
    /// This is enabled by the `curl` flag.
    pub struct CurlHttpTransport;

    fn spawn(
        options: &ClientOptions,
        receiver: Receiver<Option<Event<'static>>>,
        signal: Arc<Condvar>,
        shutdown_immediately: Arc<AtomicBool>,
        queue_size: Arc<Mutex<usize>>,
        http_client: curl::easy::Easy,
    ) {
        let dsn = options.dsn.clone().unwrap();
        let user_agent = options.user_agent.to_string();
        let http_proxy = options.http_proxy.as_ref().map(ToString::to_string);
        let https_proxy = options.https_proxy.as_ref().map(ToString::to_string);

        let mut disabled = None::<SystemTime>;
        let mut handle = http_client;

        thread::spawn(move || {
            sentry_debug!("spawning curl transport");
            let url = dsn.store_api_url().to_string();

            while let Some(event) = receiver.recv().unwrap_or(None) {
                // on drop we want to not continue processing the queue.
                if shutdown_immediately.load(Ordering::SeqCst) {
                    let mut size = queue_size.lock().unwrap();
                    *size = 0;
                    signal.notify_all();
                    break;
                }

                // while we are disabled due to rate limits, skip
                if let Some(ts) = disabled {
                    if let Ok(time_left) = ts.duration_since(SystemTime::now()) {
                        sentry_debug!(
                            "Skipping event send because we're disabled due to rate limits for {}s",
                            time_left.as_secs()
                        );
                        continue;
                    } else {
                        disabled = None;
                    }
                }

                handle.reset();
                handle.url(&url).unwrap();
                handle.custom_request("POST").unwrap();

                match (dsn.scheme(), &http_proxy, &https_proxy) {
                    (Scheme::Https, _, &Some(ref proxy)) => {
                        handle.proxy(&proxy).unwrap();
                    }
                    (_, &Some(ref proxy), _) => {
                        handle.proxy(&proxy).unwrap();
                    }
                    _ => {}
                }

                let mut body = Cursor::new(serde_json::to_vec(&event).unwrap());
                let mut retry_after = None;
                let mut headers = curl::easy::List::new();
                headers.append(&format!("X-Sentry-Auth: {}", dsn.to_auth(Some(&user_agent)))).unwrap();
                headers.append("Expect:").unwrap();
                headers.append("Content-Type: application/json").unwrap();
                handle.http_headers(headers).unwrap();
                handle.upload(true).unwrap();
                handle.in_filesize(body.get_ref().len() as u64).unwrap();
                handle.read_function(move |buf| Ok(body.read(buf).unwrap_or(0))).unwrap();
                handle.verbose(true).unwrap();
                handle.debug_function(move |info, data| {
                    let prefix = match info {
                        curl::easy::InfoType::HeaderIn => "< ",
                        curl::easy::InfoType::HeaderOut => "> ",
                        curl::easy::InfoType::DataOut => "",
                        _ => return
                    };
                    sentry_debug!("curl: {}{}", prefix, String::from_utf8_lossy(data).trim());
                }).unwrap();

                {
                    let mut handle = handle.transfer();
                    let retry_after_setter = &mut retry_after;
                    handle.header_function(move |data| {
                        if let Ok(data) = std::str::from_utf8(data) {
                            let mut iter = data.split(':');
                            if let Some(key) = iter.next().map(str::to_lowercase) {
                                if key == "retry-after" {
                                    *retry_after_setter = iter.next().map(|x| x.trim().to_string());
                                }
                            }
                        }
                        true
                    }).unwrap();
                    handle.perform().ok();
                }

                match handle.response_code() {
                    Ok(429) => {
                        if let Some(retry_after) = retry_after
                            .as_deref()
                            .and_then(parse_retry_after)
                        {
                            disabled = Some(retry_after);
                        }
                    }
                    Ok(200) | Ok(201) => {}
                    _ => {
                        sentry_debug!("Failed to send event");
                    }
                }

                let mut size = queue_size.lock().unwrap();
                *size -= 1;
                if *size == 0 {
                    signal.notify_all();
                }
            }
        })
    }

    fn http_client(_options: &ClientOptions, client: Option<curl::easy::Easy>) -> curl::easy::Easy {
        client.unwrap_or_else(curl::easy::Easy::new)
    }
}

#[cfg(feature = "surf")]
implement_http_transport! {
    /// A transport can send events via HTTP to sentry via `surf`.
    ///
    /// This is enabled by the `surf` flag.
    pub struct SurfHttpTransport;

    fn spawn(
        options: &ClientOptions,
        receiver: Receiver<Option<Event<'static>>>,
        signal: Arc<Condvar>,
        shutdown_immediately: Arc<AtomicBool>,
        queue_size: Arc<Mutex<usize>>,
        http_client: SurfClient<NativeClient>,
    ) {
        let dsn = options.dsn.clone().unwrap();
        let user_agent = options.user_agent.to_string();
        let mut disabled = None::<SystemTime>;

        thread::Builder::new()
            .name("sentry-transport".to_string())
            .spawn(move || {
                sentry_debug!("spawning surf transport");
                let http_client = http_client;
                let url = dsn.store_api_url().to_string();

                while let Some(event) = receiver.recv().unwrap_or(None) {
                    // on drop we want to not continue processing the queue.
                    if shutdown_immediately.load(Ordering::SeqCst) {
                        let mut size = queue_size.lock().unwrap();
                        *size = 0;
                        signal.notify_all();
                        break;
                    }

                    // while we are disabled due to rate limits, skip
                    if let Some(ts) = disabled {
                        if let Ok(time_left) = ts.duration_since(SystemTime::now()) {
                            sentry_debug!(
                                "Skipping event send because we're disabled due to rate limits for {}s",
                                time_left.as_secs()
                            );
                            continue;
                        } else {
                            disabled = None;
                        }
                    }

                    let req_result = http_client
                        .post(url.as_str())
                        .set_header(
                            "X-Sentry-Auth",
                            dsn.to_auth(Some(&user_agent)).to_string()
                        )
                        .body_json(&event);

                    let fut = match req_result {
                        Ok(fut) => fut,
                        Err(err) => {
                            sentry_debug!(
                                "Skipping event send because JSON serialization failed: {}",
                                err
                            );

                            continue;
                        }
                    };

                    match executor::block_on(fut) {
                        Ok(resp) => {
                            if resp.status() == 429 {
                                if let Some(retry_after) = resp
                                    .header("Retry-After")
                                    .and_then(|x| x.iter().next())
                                    .map(|x| x.as_str())
                                    .and_then(parse_retry_after)
                                {
                                    disabled = Some(retry_after);
                                }
                            }
                        }
                        Err(err) => {
                            sentry_debug!("Failed to send event: {}", err);
                        }
                    }

                    let mut size = queue_size.lock().unwrap();
                    *size -= 1;
                    if *size == 0 {
                        signal.notify_all();
                    }
                }
            }).unwrap()
    }

    fn http_client(
        _options: &ClientOptions,
        client: Option<SurfClient<NativeClient>>
    ) -> SurfClient<NativeClient> {
        client.unwrap_or_else(SurfClient::new)
    }
}

#[cfg(feature = "reqwest")]
type DefaultTransport = ReqwestHttpTransport;

#[cfg(all(feature = "curl", not(feature = "reqwest"), not(feature = "surf")))]
type DefaultTransport = CurlHttpTransport;

#[cfg(all(feature = "surf", not(feature = "reqwest"), not(feature = "curl")))]
type DefaultTransport = SurfHttpTransport;

/// The default http transport.
#[cfg(any(feature = "reqwest", feature = "curl", feature = "surf"))]
pub type HttpTransport = DefaultTransport;