-
-
Notifications
You must be signed in to change notification settings - Fork 184
feat(transport): Introduce structs for transport options #1142
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ce16615
feat(transport): Introduce structs for transport options
szokeasaurusrex 69b06e3
ref: Similar refactor for thread transport
szokeasaurusrex 4a3885a
fix: feature-guard accept_invalid_certs
szokeasaurusrex cd693aa
fixup! fix: feature-guard accept_invalid_certs
szokeasaurusrex File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| //! Includes the [`TransportOptions`] struct. | ||
|
|
||
| use std::borrow::Cow; | ||
|
|
||
| use sentry_types::Dsn; | ||
|
|
||
| use crate::ClientOptions; | ||
|
|
||
| /// Options for a transport. | ||
| #[derive(Debug)] | ||
| #[must_use] | ||
| #[non_exhaustive] | ||
| pub struct TransportOptions { | ||
|
lcian marked this conversation as resolved.
lcian marked this conversation as resolved.
|
||
| /// The transport's Sentry DSN. | ||
| pub dsn: Dsn, | ||
| /// The user agent sent with transport requests. | ||
| pub user_agent: Cow<'static, str>, | ||
| /// An optional HTTP proxy to use. | ||
| pub http_proxy: Option<Cow<'static, str>>, | ||
| /// An optional HTTPS proxy to use. | ||
| pub https_proxy: Option<Cow<'static, str>>, | ||
| /// Whether TLS certificate validation should be disabled. | ||
| pub accept_invalid_certs: bool, | ||
| } | ||
|
|
||
| impl TransportOptions { | ||
| /// Try to convert a [`&ClientOptions`](ClientOptions) to a [`TransportOptions`] by extracting | ||
| /// the relevant fields from the `ClientOptions`. | ||
| /// | ||
| /// This method is provided so that code which expects [`TransportOptions`] can be | ||
| /// backwards-compatible with older code, which provides `ClientOptions`. | ||
| /// | ||
| /// Returns [`None`] if `options.dsn` is `None`, `Some(_)` otherwise. | ||
| pub fn try_from_client_options(options: &ClientOptions) -> Option<Self> { | ||
| let ClientOptions { | ||
| dsn, | ||
| http_proxy, | ||
| https_proxy, | ||
| accept_invalid_certs, | ||
| user_agent, | ||
| .. | ||
| } = options; | ||
|
|
||
| dsn.as_ref().cloned().map(|dsn| Self { | ||
| dsn, | ||
| user_agent: user_agent.clone(), | ||
| http_proxy: http_proxy.clone(), | ||
| https_proxy: https_proxy.clone(), | ||
| accept_invalid_certs: *accept_invalid_certs, | ||
| }) | ||
| } | ||
|
|
||
| /// Converts these [`TransportOptions`] into [`ClientOptions`]. | ||
| /// | ||
| /// This method is provided for backwards-compatibility with custom transports which cannot | ||
| /// be contructed from [`TransportOptions`] because they expect [`ClientOptions`]. | ||
| /// | ||
| /// Any fields on [`ClientOptions`] which are not present in [`TransportOptions`] will be | ||
| /// set to their default values. | ||
| pub(crate) fn into_client_options(self) -> ClientOptions { | ||
| let Self { | ||
| dsn, | ||
| user_agent, | ||
| http_proxy, | ||
| https_proxy, | ||
| accept_invalid_certs, | ||
| } = self; | ||
|
|
||
| let dsn = Some(dsn); | ||
|
|
||
| ClientOptions { | ||
| dsn, | ||
| user_agent, | ||
| http_proxy, | ||
| https_proxy, | ||
| accept_invalid_certs, | ||
| ..Default::default() | ||
| } | ||
| } | ||
|
Comment on lines
+69
to
+79
This comment was marked as spam.
Sorry, something went wrong. |
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,8 +2,12 @@ use std::io::{Cursor, Read}; | |
| use std::time::Duration; | ||
|
|
||
| use curl::easy::Easy as CurlClient; | ||
| use sentry_core::TransportOptions; | ||
|
|
||
| use super::{thread::TransportThread, HTTP_PAYLOAD_TOO_LARGE, HTTP_PAYLOAD_TOO_LARGE_MESSAGE}; | ||
| use super::{ | ||
| thread::{TransportThread, TransportThreadOptions}, | ||
| RateLimiter, HTTP_PAYLOAD_TOO_LARGE, HTTP_PAYLOAD_TOO_LARGE_MESSAGE, | ||
| }; | ||
|
|
||
| use crate::{sentry_debug, types::Scheme, ClientOptions, Envelope, Transport}; | ||
|
|
||
|
|
@@ -15,30 +19,78 @@ pub struct CurlHttpTransport { | |
| thread: TransportThread, | ||
| } | ||
|
|
||
| /// Options for constructing a [`CurlHttpTransport`]. | ||
| /// | ||
| /// Currently, this is primarily a wrapper around a [`TransportOptions`], and must be created with | ||
| /// the `From<TransportOptions>` implementation. Optionally, a [`curl::easy::Easy`] client for the | ||
| /// transport may be provided with [`Self::with_client`]. | ||
| #[derive(Debug)] | ||
| #[must_use] | ||
| pub struct CurlHttpTransportOptions { | ||
| general_options: TransportOptions, | ||
| client: Option<CurlClient>, | ||
| } | ||
|
|
||
| impl CurlHttpTransport { | ||
| /// Creates a new Transport. | ||
| /// Backwards-compatible method for creating a [`CurlHttpTransport`]. | ||
| /// | ||
| /// Please use [`CurlHttpTransportOptions::build`] instead. | ||
| /// | ||
| /// ### Panics | ||
| /// | ||
| /// Panics if called with `options` that lack a DSN. | ||
|
Comment on lines
+39
to
+41
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This panic is not new. It was previously undocumented. |
||
| #[inline] | ||
| #[deprecated = "use `CurlHttpTransportOptions::build` instead"] | ||
| pub fn new(options: &ClientOptions) -> Self { | ||
| Self::new_internal(options, None) | ||
| let general_options = TransportOptions::try_from_client_options(options) | ||
| .expect("this method should only be called when options has a DSN"); | ||
|
|
||
| CurlHttpTransportOptions::from(general_options).build() | ||
| } | ||
|
|
||
| /// Creates a new Transport that uses the specified [`CurlClient`]. | ||
| /// Backwards-compatible method for creating a [`CurlHttpTransport`] that uses the specified | ||
| /// [`CurlClient`]. | ||
| /// | ||
| /// Please use [`CurlHttpTransportOptions::build`] instead. | ||
| /// | ||
| /// ### Panics | ||
| /// | ||
| /// Panics if called with `options` that lack a DSN. | ||
| #[inline] | ||
| #[deprecated = "use `CurlHttpTransportOptions::build` instead"] | ||
| pub fn with_client(options: &ClientOptions, client: CurlClient) -> Self { | ||
| Self::new_internal(options, Some(client)) | ||
| let general_options = TransportOptions::try_from_client_options(options) | ||
| .expect("this method should only be called when options has a DSN"); | ||
|
|
||
| CurlHttpTransportOptions::from(general_options) | ||
| .with_client(client) | ||
| .build() | ||
| } | ||
|
|
||
| fn new_internal(options: &ClientOptions, client: Option<CurlClient>) -> Self { | ||
| /// Creates a new [`CurlHttpTransport`] with the given `options`. | ||
| #[inline] | ||
| pub(super) fn with_options(options: CurlHttpTransportOptions) -> Self { | ||
| let CurlHttpTransportOptions { | ||
| general_options: | ||
| TransportOptions { | ||
| dsn, | ||
| user_agent, | ||
| http_proxy, | ||
| https_proxy, | ||
| accept_invalid_certs, | ||
| .. | ||
| }, | ||
| client, | ||
| } = options; | ||
|
|
||
| let client = client.unwrap_or_else(CurlClient::new); | ||
| 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 dsn = options.dsn.as_ref().unwrap(); | ||
| let user_agent = options.user_agent.clone(); | ||
| let auth = dsn.to_auth(Some(&user_agent)).to_string(); | ||
| let url = dsn.envelope_api_url().to_string(); | ||
| let scheme = dsn.scheme(); | ||
| let accept_invalid_certs = options.accept_invalid_certs; | ||
|
|
||
| let mut handle = client; | ||
| let thread = TransportThread::new(move |envelope, rl| { | ||
|
|
||
| let send_fn = move |envelope: Envelope, rl: &mut RateLimiter| { | ||
| handle.reset(); | ||
| handle.url(&url).unwrap(); | ||
| handle.custom_request("POST").unwrap(); | ||
|
|
@@ -130,7 +182,9 @@ impl CurlHttpTransport { | |
| sentry_debug!("Failed to send envelope: {}", err); | ||
| } | ||
| } | ||
| }); | ||
| }; | ||
|
|
||
| let thread = TransportThreadOptions::new(send_fn).spawn_thread(); | ||
| Self { thread } | ||
| } | ||
| } | ||
|
|
@@ -147,3 +201,28 @@ impl Transport for CurlHttpTransport { | |
| self.flush(timeout) | ||
| } | ||
| } | ||
|
|
||
| impl From<TransportOptions> for CurlHttpTransportOptions { | ||
| #[inline] | ||
| fn from(value: TransportOptions) -> Self { | ||
| Self { | ||
| general_options: value, | ||
| client: None, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl CurlHttpTransportOptions { | ||
| /// Specify the [`CurlClient`] for the [`CurlHttpTransport`]. | ||
| #[inline] | ||
| pub fn with_client(self, client: CurlClient) -> Self { | ||
| let client = Some(client); | ||
| Self { client, ..self } | ||
| } | ||
|
|
||
| /// Create a [`CurlHttpTransport`] using these options. | ||
| #[inline] | ||
| pub fn build(self) -> CurlHttpTransport { | ||
| CurlHttpTransport::with_options(self) | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.