0

I was wondering why this piece of code always exit with 1:

import Foundation
// an idn domain:
let uLabel = "համընդհանուր-ընկալում-թեստ.հայ"
let urlStr = "https://" + uLabel
guard let url = URL(string: urlStr) else { exit(1) }
exit(0)

Since Apple's browser Safari does support well IDN domains, I was surprised their URL library does not... I tried to urlencode the string beforehand, but it is not helping.

======EDIT======

After fixing the piece above upon Matt's suggestion, I faced another problem during fetching the website data:

import Foundation

let uLabel = "համընդհանուր-ընկալում-թեստ.հայ"
let scheme = "https"
var comps = URLComponents()
comps.scheme = scheme
comps.host = uLabel
guard let url = comps.url else { exit(1) }
let task = URLSession.shared.dataTask(with: url) { data, response, error in
    guard let rawContent = data else { exit(1) }
    guard let content = String(data: rawContent, encoding: String.Encoding.utf8) else { exit(1) }
    if content.contains("UASG Testbed Landing Page") {
        // successfully fetch content of the page
        exit(0)
    } else {
        // error during fetching
        exit(1)
    }
}
task.resume()
RunLoop.main.run()

The program still exits with 1. It seems the domain is not converted to an A-LABEL as it is on Safari, as the error suggests (the certificate is valid, the error is misleading):

NSLocalizedDescription=The certificate for this server is invalid. You might be connecting to a server that is pretending to be “համընդհանուր-ընկալում-թեստ.հայ” which could put your confidential information at risk., NSErrorFailingURLKey=https://%d5%b0%d5%a1%d5%b4%d5%a8%d5%b6%d5%a4%d5%b0%d5%a1%d5%b6%d5%b8%d6%82%d6%80-%d5%a8%d5%b6%d5%af%d5%a1%d5%ac%d5%b8%d6%82%d5%b4-%d5%a9%d5%a5%d5%bd%d5%bf.%d5%b0%d5%a1%d5%b5/,

5
  • 1
    I answer your question so instead of accepting my answer you change the question? That's a thing to do?
    – matt
    Nov 13, 2021 at 2:35
  • I certainly upvote your interesting answer (and order your book ;) ). But I was not looking for a way to simply elude the nullable exit, but more a way to tell the URL framework to correctly represent the url internally (by an A-LABEL probably). With URLComponents, the domain is urlencoded or badly translated and does not represent anymore the address I wanted to query... In this sense, I prefer the URL(string: ) that doesn't fail silently/stangely like that. Nov 15, 2021 at 14:35
  • It's good you figured something out! If you think there's an issue with what Apple is doing here, you should please file a bug report with them.
    – matt
    Nov 15, 2021 at 15:00
  • 1
    I log a bug to Apple's feedback assistant. But it is possible the bug is in the open-source part of the Foundation library, so I opened it here too: bugs.swift.org/browse/SR-15487 Nov 16, 2021 at 15:13
  • Nice, thanks for doing those.
    – matt
    Nov 16, 2021 at 16:32

2 Answers 2

2

I don't know why you're having trouble, but rule number one is never never never call URL(string). Use URLComponents. That's what it's for.

let uLabel = "համընդհանուր-ընկալում-թեստ.հայ"
let scheme = "https"
var comps = URLComponents()
comps.scheme = scheme
comps.host = uLabel
let url = comps.url // works for me
4
  • Thanks a lot for this proposition. Can you elaborate on why this URL(string) is not appropriate? Also, this piece of code was the preparation before fetching the website data, but it still seems to fail, see my edit. Nov 12, 2021 at 20:45
  • The issue with using URLComponents is that it wrongly URL-encodes the host instead of IDNA-encoding it.
    – Wevah
    Feb 7 at 19:34
  • @Wevah Yeah, that's why (a) the other answer is accepted and (b) I upvoted it. :)
    – matt
    Feb 7 at 20:13
  • Aha; missed all the back-and-forth.
    – Wevah
    Feb 8 at 21:37
1

Until, someone can find a way to tell the URL framework to correctly represent the URL "https://համընդհանուր-ընկալում-թեստ.հայ", it is possible to translate the domain part to an A-LABEL before passing it to the URL constructor and elude its wrong internal representation:

import Foundation
import IDNA

// an idn domain:
let uLabel = "համընդհանուր-ընկալում-թեստ.հայ"
guard let aLabel = uLabel.idnaEncoded else { exit(1) }
let supportedUrl = "https://" + aLabel
guard let url = URL(string: supportedUrl) else { exit(1) }
let task = URLSession.shared.dataTask(with: url) { data, response, error in
    guard let rawContent = data else { exit(1) }
    guard let content = String(data: rawContent, encoding: String.Encoding.utf8) else { exit(1) }
    if content.contains("UASG Testbed Landing Page") {
        // successfully fetch content of the page
        exit(0)
    } else {
        // error during fetching
        exit(1)
    }
}
task.resume()
RunLoop.main.run()

This piece of code does exit 0. The IDNA library is there (ensure you take the master branch, because released versions are still on IDNA2003):

https://github.com/Wevah/IDNA-Cocoa

1
  • Please report this to Apple as a bug Nov 15, 2021 at 22:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.