URLSession default User-Agent

URLSession default User-Agent

In the past I have blogged how I have developed a POC of various types of downloads in SwiftUI:

Recently the application stopped working, without any change in my code.
Even more, the app was deployed on test devices, so it was not even recompiled with newer versions of Xcode.
I thought about some server error, I've opened an issue the Apple Developer Forum (later the link) but I didn't receive any answer, so I forgot about it.

Recently I wanted to test MAUI deployments on devices (MAUI is the Microsoft framework to develop cross-platform applications) and I thought to test the download of the sample files from https://speed.hetzner.de/, and with my surprise, they worked.
So I did more test, and noticed that .NET/MAUI downloads were working both on computer and on devices (iPhones), while the code written in Swift was not working on both computers and devices.

Very strange... so I added all my observations to the issue in the Apple Developer Forum, and luckily I got a complete answer: essentially, the URLSession sends a custom user-agent, that is dependent on the name of the application, and the server doesn't accept these custom user-agents anymore, refusing (i.e. aborting) the http connection.

I never knew the URLSession sends by default a custom user-agent... never found any documentation about it, and doing specific researches, I've found this Stack Overflow issue.
Just for reference, the default user agents are something like

$(CFBundleName)/$(CFBundleVersion) CFNetwork/808.3 Darwin/16.3.0

Really strange, anyway it has been enough to clear it, and the downloads have started to work again!

let config = URLSessionConfiguration.default
config.httpAdditionalHeaders = ["User-Agent": ""]
...

Btw, the answer in the issue in the Apple Developer Forum contained some very interesting links to read: