Replies: 1 comment 6 replies
-
So, this is what you need to redirect a request: class CustomURLProtocol: URLProtocol {
override class func canInit(with request: URLRequest) -> Bool {
return URLProtocol.property(forKey: "Handled", in: request) == nil
}
override class func canonicalRequest(for request: URLRequest) -> URLRequest {
guard let newUrl = URL(string: "https://redirectUrl.com/forwardSentry") else { return request }
var newRequest = URLRequest(url: newUrl)
newRequest.httpBody = request.httpBody
newRequest.httpMethod = request.httpMethod
newRequest.allHTTPHeaderFields = request.allHTTPHeaderFields
return newRequest
}
override func startLoading() {
URLProtocol.setProperty(true, forKey: "Handled", in: request as! NSMutableURLRequest)
let newTask = URLSession.shared.dataTask(with: request) { data, response, error in
if let data = data { self.client?.urlProtocol(self, didLoad: data) }
if let response = response { self.client?.urlProtocol(self, didReceive: response, cacheStoragePolicy: .notAllowed) }
if let error = error { self.client?.urlProtocol(self, didFailWithError: error) }
self.client?.urlProtocolDidFinishLoading(self)
}
newTask.resume()
}
override func stopLoading() {
// Not implemented on purpose
}
}
static func startSentry() {
var config = URLSessionConfiguration.default
config.protocolClasses = [CustomURLProtocol.self]
SentrySDK.start { options in
options.dsn = "..."
options.urlSession = URLSession(configuration: config)
}
} You can call |
Beta Was this translation helpful? Give feedback.
6 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello,
I am having trouble getting a URLSession to work, since our sentry instance is not reachable from public internet, I want to route the traffic first to a public facing server, which will act as a proxy.
Please note I am not really a Swift developer, I only know a few things here and there. ;)
With #3811 its possible to define a own URLSession, and to my understanding can be used in a more-or-less similar way as the
tunnel
option from the Sentry JS SDK.So I've created a delegate class for URLSession like that:
And setup the Sentry iOS SDK (
8.28
) like that in our AppDelegate:and put some breakpoints on the OmSentry overwritten functions to see if they are triggered.
However, they never are triggered. In all cases they arent.
My goal is to fix the "fakedsn" (on the SentrySDK configuration part) to a new URL which will receive the envelope data as-is and simply forwards that data (after checks) to the actual sentry instance.
Thanks for reading!
Beta Was this translation helpful? Give feedback.
All reactions