Skip to content

Conversation

@NikGariel
Copy link

  • Modified WebAppController.swift to check for .ton domains and convert to tonsite scheme
  • Added TonSchemeHandler in WebAppWebView.swift to handle tonsite URL scheme
  • Ensures proper loading of .ton URLs without introducing new features

- Modified WebAppController.swift to check for .ton domains and convert to tonsite scheme
- Added TonSchemeHandler in WebAppWebView.swift to handle tonsite URL scheme
- Ensures proper loading of .ton URLs without introducing new features

Handle .ton domains correctly in WebAppController and WebAppWebView

- Modified WebAppController.swift to check for .ton domains and convert to tonsite scheme
- Added TonSchemeHandler in WebAppWebView.swift to handle tonsite URL scheme
- Ensures proper loading of .ton URLs without introducing new features
Comment on lines +434 to +445
if let host = parsedUrl.host, host.lowercased().hasSuffix(".ton") {
// Convert to tonsite scheme
var urlComponents = URLComponents(string: url)
urlComponents?.scheme = "tonsite"
if let updatedUrl = urlComponents?.url {
self.webView?.load(URLRequest(url: updatedUrl))
} else {
self.webView?.load(URLRequest(url: parsedUrl))
}
} else {
self.webView?.load(URLRequest(url: parsedUrl))
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this be simplified like this to avoid multiple if-else statements ?

let urlToLoad: URL
if parsedUrl.host?.lowercased().hasSuffix(".ton") == true,
   var components = URLComponents(url: parsedUrl, resolvingAgainstBaseURL: false) {
    components.scheme = "tonsite"
    urlToLoad = components.url ?? parsedUrl
} else {
    urlToLoad = parsedUrl
}

self.webView?.load(URLRequest(url: urlToLoad))

Comment on lines +469 to +480
if let host = parsedUrl.host, host.lowercased().hasSuffix(".ton") {
// Convert to tonsite scheme
var urlComponents = URLComponents(string: result.url)
urlComponents?.scheme = "tonsite"
if let updatedUrl = urlComponents?.url {
strongSelf.webView?.load(URLRequest(url: updatedUrl))
} else {
strongSelf.webView?.load(URLRequest(url: parsedUrl))
}
} else {
strongSelf.webView?.load(URLRequest(url: parsedUrl))
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this probably also could be simplified.

let urlToLoad: URL
if parsedUrl.host?.lowercased().hasSuffix(".ton") == true,
   var components = URLComponents(url: parsedUrl, resolvingAgainstBaseURL: false) {
    components.scheme = "tonsite"
    urlToLoad = components.url ?? parsedUrl
} else {
    urlToLoad = parsedUrl
}

strongSelf.webView?.load(URLRequest(url: urlToLoad))

Comment on lines +522 to +535
// Check if the URL has a .ton domain
if let host = parsedUrl.host, host.lowercased().hasSuffix(".ton") {
// Convert to tonsite scheme
var urlComponents = URLComponents(string: result.url)
urlComponents?.scheme = "tonsite"
if let updatedUrl = urlComponents?.url {
strongSelf.webView?.load(URLRequest(url: updatedUrl))
} else {
strongSelf.webView?.load(URLRequest(url: parsedUrl))
}
} else {
strongSelf.webView?.load(URLRequest(url: parsedUrl))
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also could be simplified

Comment on lines +501 to +512
if let host = parsedUrl.host, host.lowercased().hasSuffix(".ton") {
// Convert to tonsite scheme
var urlComponents = URLComponents(string: result.url)
urlComponents?.scheme = "tonsite"
if let updatedUrl = urlComponents?.url {
self.webView?.load(URLRequest(url: updatedUrl))
} else {
self.webView?.load(URLRequest(url: parsedUrl))
}
} else {
self.webView?.load(URLRequest(url: parsedUrl))
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also could be simplified

mappedPath = "/\(mappedPath)"
}
}
let mappedUrl = "https://\(mappedHost).\(proxyServerHost)\(mappedPath)"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would use here a function like this

private static func mappedUrl(from url: URL?, proxyHost: String) -> URL {
        let mappedHost = url?.host?
            .replacingOccurrences(of: "-", with: "-h")
            .replacingOccurrences(of: ".", with: "-d") ?? ""

        let mappedPath: String
        if let path = url?.path, !path.isEmpty {
            mappedPath = path.hasPrefix("/") ? path : "/" + path
        } else {
            mappedPath = ""
        }

        return URL(string: "https://\(mappedHost).\(proxyHost)\(mappedPath)")!
    }

}
let mappedUrl = "https://\(mappedHost).\(proxyServerHost)\(mappedPath)"
let isCompleted = self.isCompleted
self.urlSessionTask = URLSession.shared.dataTask(with: URLRequest(url: URL(string: mappedUrl)!), completionHandler: { data, response, error in

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
self.urlSessionTask = URLSession.shared.dataTask(with: URLRequest(url: URL(string: mappedUrl)!), completionHandler: { data, response, error in
self.urlSessionTask = URLSession.shared.dataTask(with: URLRequest(url: url: mappedUrl), completionHandler: { data, response, error in

You can the the output of the function above that I suggested mappedUrl(from url: URL?, proxyHost: String) -> URL

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants