Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
- Crashes for uncaught NSExceptions will now report the stracktrace recorded within the exception (#5306)
- Move SentryExperimentalOptions to a property defined in Swift (#5329)
- Add redaction in session replay for `SFSafariView` used by `SFSafariViewController` and `ASWebAuthenticationSession` (#5408)
- Convert SentryNSURLRequest to Swift (#5451)

### Fixes

Expand Down
28 changes: 12 additions & 16 deletions Sentry.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

15 changes: 6 additions & 9 deletions Sources/Sentry/SentryHttpTransport.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
#import "SentryEvent.h"
#import "SentryFileManager.h"
#import "SentryLog.h"
#import "SentryNSURLRequest.h"
#import "SentryNSURLRequestBuilder.h"
#import "SentryOptions.h"
#import "SentrySerialization.h"
#import "SentrySwift.h"
Expand Down Expand Up @@ -339,14 +337,13 @@ - (void)sendAllCachedEnvelopes
rateLimitedEnvelope.header.sentAt = SentryDependencyContainer.sharedInstance.dateProvider.date;

NSError *requestError = nil;
NSURLRequest *request = [self.requestBuilder createEnvelopeRequest:rateLimitedEnvelope
dsn:self.options.parsedDsn
didFailWithError:&requestError];
NSURLRequest *request =
[self.requestBuilder createEnvelopeRequestWithEnvelope:rateLimitedEnvelope
dsn:self.options.parsedDsn
error:&requestError];

if (nil == request || nil != requestError) {
if (nil != requestError) {
SENTRY_LOG_DEBUG(@"Failed to build request: %@.", requestError);
}
if (nil != requestError) {
SENTRY_LOG_DEBUG(@"Failed to build request: %@.", requestError);
[self recordLostEventFor:rateLimitedEnvelope.items];
[self deleteEnvelopeAndSendNext:envelopeFilePath];
return;
Expand Down
85 changes: 0 additions & 85 deletions Sources/Sentry/SentryNSURLRequest.m

This file was deleted.

42 changes: 0 additions & 42 deletions Sources/Sentry/SentryNSURLRequestBuilder.m

This file was deleted.

15 changes: 6 additions & 9 deletions Sources/Sentry/SentrySpotlightTransport.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
#import "SentryEnvelopeItemHeader.h"
#import "SentryEnvelopeItemType.h"
#import "SentryLog.h"
#import "SentryNSURLRequest.h"
#import "SentryNSURLRequestBuilder.h"
#import "SentryOptions.h"
#import "SentrySerialization.h"
#import "SentrySwift.h"
#import "SentryTransport.h"

NS_ASSUME_NONNULL_BEGIN
Expand Down Expand Up @@ -66,14 +65,12 @@ - (void)sendEnvelope:(SentryEnvelope *)envelope
items:allowedEnvelopeItems];

NSError *requestError = nil;
NSURLRequest *request = [self.requestBuilder createEnvelopeRequest:envelopeToSend
url:self.apiURL
didFailWithError:&requestError];
NSURLRequest *request = [self.requestBuilder createEnvelopeRequestWithEnvelope:envelopeToSend
url:self.apiURL
error:&requestError];

if (nil == request || nil != requestError) {
if (nil != requestError) {
SENTRY_LOG_ERROR(@"Unable to build envelope request with error %@", requestError);
}
if (nil != requestError) {
SENTRY_LOG_ERROR(@"Unable to build envelope request with error %@", requestError);
return;
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/Sentry/SentryTransportFactory.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
#import "SentryEnvelopeRateLimit.h"
#import "SentryHttpDateParser.h"
#import "SentryHttpTransport.h"
#import "SentryNSURLRequestBuilder.h"
#import "SentryOptions.h"
#import "SentryQueueableRequestManager.h"
#import "SentryRateLimitParser.h"
#import "SentryRateLimits.h"

#import "SentryRetryAfterHeaderParser.h"
#import "SentrySpotlightTransport.h"
#import "SentrySwift.h"
#import "SentryTransport.h"

NS_ASSUME_NONNULL_BEGIN
Expand Down
21 changes: 0 additions & 21 deletions Sources/Sentry/include/SentryNSURLRequest.h

This file was deleted.

23 changes: 0 additions & 23 deletions Sources/Sentry/include/SentryNSURLRequestBuilder.h

This file was deleted.

1 change: 1 addition & 0 deletions Sources/Sentry/include/SentryPrivate.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#import "NSLocale+Sentry.h"
#import "SentryCrashExceptionApplicationHelper.h"
#import "SentryDispatchQueueWrapper.h"
#import "SentryEnvelope.h"
#import "SentryNSDataUtils.h"
#import "SentryRandom.h"
#import "SentryTime.h"
Expand Down
27 changes: 27 additions & 0 deletions Sources/Swift/Tools/SentryURLRequestBuilder.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
@_implementationOnly import _SentryPrivate

private enum Error: Swift.Error {
case serializationError
}

@objc(SentryNSURLRequestBuilder)
@_spi(Private) public class SentryURLRequestBuilder: NSObject {

@objc
public func createEnvelopeRequest(envelope: SentryEnvelope, dsn: SentryDsn) throws -> URLRequest {

Check failure on line 11 in Sources/Swift/Tools/SentryURLRequestBuilder.swift

View workflow job for this annotation

GitHub Actions / Check no UIKit linkage (ReleaseWithoutUIKit)

cannot use class 'SentryEnvelope' here; '_SentryPrivate' has been imported as implementation-only

Check failure on line 11 in Sources/Swift/Tools/SentryURLRequestBuilder.swift

View workflow job for this annotation

GitHub Actions / Check no UIKit linkage (ReleaseWithoutUIKit)

cannot use class 'SentryEnvelope' here; '_SentryPrivate' has been imported as implementation-only

Check failure on line 11 in Sources/Swift/Tools/SentryURLRequestBuilder.swift

View workflow job for this annotation

GitHub Actions / Check no UIKit linkage (DebugWithoutUIKit)

cannot use class 'SentryEnvelope' here; '_SentryPrivate' has been imported as implementation-only

Check failure on line 11 in Sources/Swift/Tools/SentryURLRequestBuilder.swift

View workflow job for this annotation

GitHub Actions / Check no UIKit linkage (DebugWithoutUIKit)

cannot use class 'SentryEnvelope' here; '_SentryPrivate' has been imported as implementation-only

Check failure on line 11 in Sources/Swift/Tools/SentryURLRequestBuilder.swift

View workflow job for this annotation

GitHub Actions / build-xcframework-variant-slices (Sentry, mh_dylib, -WithoutUIKitOrAppKit, WithoutUIKit, sentry-w... / iphoneos

cannot use class 'SentryEnvelope' here; '_SentryPrivate' has been imported as implementation-only

Check failure on line 11 in Sources/Swift/Tools/SentryURLRequestBuilder.swift

View workflow job for this annotation

GitHub Actions / build-xcframework-variant-slices (Sentry, mh_dylib, -WithoutUIKitOrAppKit, WithoutUIKit, sentry-w... / iphoneos

cannot use class 'SentryEnvelope' here; '_SentryPrivate' has been imported as implementation-only
guard let data = SentrySerialization.data(with: envelope) else {
SentryLog.error("Envelope cannot be converted to data")
throw Error.serializationError
}
return try SentryURLRequestFactory.envelopeRequest(with: dsn, data: data)
}

@objc
public func createEnvelopeRequest(envelope: SentryEnvelope, url: URL) throws -> URLRequest {

Check failure on line 20 in Sources/Swift/Tools/SentryURLRequestBuilder.swift

View workflow job for this annotation

GitHub Actions / Check no UIKit linkage (ReleaseWithoutUIKit)

cannot use class 'SentryEnvelope' here; '_SentryPrivate' has been imported as implementation-only

Check failure on line 20 in Sources/Swift/Tools/SentryURLRequestBuilder.swift

View workflow job for this annotation

GitHub Actions / Check no UIKit linkage (ReleaseWithoutUIKit)

cannot use class 'SentryEnvelope' here; '_SentryPrivate' has been imported as implementation-only

Check failure on line 20 in Sources/Swift/Tools/SentryURLRequestBuilder.swift

View workflow job for this annotation

GitHub Actions / Check no UIKit linkage (DebugWithoutUIKit)

cannot use class 'SentryEnvelope' here; '_SentryPrivate' has been imported as implementation-only

Check failure on line 20 in Sources/Swift/Tools/SentryURLRequestBuilder.swift

View workflow job for this annotation

GitHub Actions / Check no UIKit linkage (DebugWithoutUIKit)

cannot use class 'SentryEnvelope' here; '_SentryPrivate' has been imported as implementation-only

Check failure on line 20 in Sources/Swift/Tools/SentryURLRequestBuilder.swift

View workflow job for this annotation

GitHub Actions / build-xcframework-variant-slices (Sentry, mh_dylib, -WithoutUIKitOrAppKit, WithoutUIKit, sentry-w... / iphoneos

cannot use class 'SentryEnvelope' here; '_SentryPrivate' has been imported as implementation-only

Check failure on line 20 in Sources/Swift/Tools/SentryURLRequestBuilder.swift

View workflow job for this annotation

GitHub Actions / build-xcframework-variant-slices (Sentry, mh_dylib, -WithoutUIKitOrAppKit, WithoutUIKit, sentry-w... / iphoneos

cannot use class 'SentryEnvelope' here; '_SentryPrivate' has been imported as implementation-only
guard let data = SentrySerialization.data(with: envelope) else {
SentryLog.error("Envelope cannot be converted to data")
throw Error.serializationError
}
return try SentryURLRequestFactory.envelopeRequest(with: url, data: data, authHeader: nil)
}
}
60 changes: 60 additions & 0 deletions Sources/Swift/Tools/SentryURLRequestFactory.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
@_implementationOnly import _SentryPrivate
import Foundation

private enum Error: Swift.Error {
case jsonConversionError
}

enum SentryURLRequestFactory {

private static let serverVersionString = "7"
private static let requestTimeout: TimeInterval = 15

static func envelopeRequest(with dsn: SentryDsn, data: Data) throws -> URLRequest {
let apiURL = dsn.getEnvelopeEndpoint()
let authHeader = Self.newAuthHeader(url: dsn.url)

return try Self.envelopeRequest(with: apiURL, data: data, authHeader: authHeader)
}

static func envelopeRequest(with url: URL, data: Data, authHeader: String?) throws -> URLRequest {
var request = URLRequest(url: url, cachePolicy: .reloadIgnoringLocalCacheData, timeoutInterval: Self.requestTimeout)

request.httpMethod = "POST"

if let authHeader = authHeader {
request.setValue(authHeader, forHTTPHeaderField: "X-Sentry-Auth")
}
request.setValue("application/x-sentry-envelope", forHTTPHeaderField: "Content-Type")
request.setValue("\(SentryMeta.sdkName)/\(SentryMeta.versionString)", forHTTPHeaderField: "User-Agent")
request.setValue("gzip", forHTTPHeaderField: "Content-Encoding")

var error: NSError?
let data = sentry_gzippedWithCompressionLevel(data, -1, &error)
if let error {
SentryLog.log(message: "Failed to compress envelope request body: \(error)", andLevel: .error)
throw error

Check warning on line 36 in Sources/Swift/Tools/SentryURLRequestFactory.swift

View check run for this annotation

Codecov / codecov/patch

Sources/Swift/Tools/SentryURLRequestFactory.swift#L35-L36

Added lines #L35 - L36 were not covered by tests
}
request.httpBody = data

SentryLog.log(message: "Constructed request: \(self)", andLevel: .debug)
return request
}

private static func newHeaderPart(key: String, value: Any) -> String {
return "\(key)=\(value)"
}

private static func newAuthHeader(url: URL) -> String {
var string = "Sentry "
string += newHeaderPart(key: "sentry_version", value: serverVersionString) + ","
string += newHeaderPart(key: "sentry_client", value: "\(SentryMeta.sdkName)/\(SentryMeta.versionString)") + ","
string += newHeaderPart(key: "sentry_key", value: url.user ?? "")

if let password = url.password {
string += "," + newHeaderPart(key: "sentry_secret", value: password)
}

return string
}
}
2 changes: 1 addition & 1 deletion Tests/SentryTests/Networking/SentryDsnTests.m
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#import "SentryDsn.h"
#import "SentryError.h"
#import "SentryMeta.h"
#import "SentryNSURLRequest.h"
#import "SentryOptions+HybridSDKs.h"
#import "SentrySwift.h"
#import <XCTest/XCTest.h>

@interface SentryDsnTests : XCTestCase
Expand Down
Loading
Loading