-
Notifications
You must be signed in to change notification settings - Fork 1.2k
[SR-14677] FoundationNetworking web request error on Debian/TwisterOS #3959
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
@swift-ci create |
Recently, when developing Swift programs on Linux, I encountered a situation where the same network request worked fine with the The scenario is as follows: at a certain point in time, a series of network requests need to be executed. The first network request works perfectly, but the second one starts to fail with the previously mentioned error, and some other requests also fail immediately, with most of the errors being In order to investigate the issue, I switched to using the I was truly frustrated with this issue and started various attempts, including examining the open-source implementation of URLSession, which Apple graciously made available. Finally, I found a solution. Let me share it with you: // Or modify it to your preferred encoding strategy
request.addValue("*", forHTTPHeaderField: "Accept-Encoding") Yes, it's as simple as adding an accepted encoding to the header. Why? Oh my goodness! There are actually three reasons behind this:
Here's the analysis:
// Sources/FoundationNetworking/URLSession/libcurl/MultiHandle.swift
case (CFURLSessionEasyCodeABORTED_BY_CALLBACK, _):
return NSURLErrorUnknown // Or NSURLErrorCancelled if we're in such a state The culprit was in the // `swift-corelibs-foundation/Sources/FoundationNetworking/URLSession/libcurl/EasyHandle.swift`
func set(automaticBodyDecompression flag: Bool) {
if flag {
"".withCString {
try! CFURLSession_easy_setopt_ptr(rawHandle, CFURLSessionOptionACCEPT_ENCODING, UnsafeMutableRawPointer(mutating: $0)).asError()
}
try! CFURLSession_easy_setopt_long(rawHandle, CFURLSessionOptionHTTP_CONTENT_DECODING, 1).asError()
} else {
try! CFURLSession_easy_setopt_ptr(rawHandle, CFURLSessionOptionACCEPT_ENCODING, nil).asError()
try! CFURLSession_easy_setopt_long(rawHandle, CFURLSessionOptionHTTP_CONTENT_DECODING, 0).asError()
}
}
// CoreFoundation/URL.subproj/CFURLSessionInterface.c
CFURLSessionOption const CFURLSessionOptionACCEPT_ENCODING = { CURLOPT_ACCEPT_ENCODING }; Everywhere this method was called, it was set to true, indicating that this functionality was designed without considering situations where the encoding strategy might not match. I had previously seen issues with FTP services failing with URLSession, and I believe it's for the same reason. |
Environment
Raspberry PI 4, 4gb ram, Debian/TwisterOS, swift5.1
Additional Detail from JIRA
md5: f2166f3f1cc9ddf3c5fe1e91bb8d9745
Issue Description:
I am using the following code to get the content from a website. On macOS this works fine, but on the Pi 4 this only throws <Error Domain=NSURLErrorDomain Code=-1 "(null)">. ** I also tried several other ways to download the website content, but they are all throwing the same error on the pi while working fine on the Mac.
Anny ideas how to fit that issue ?
import Foundation **
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif
#if os(Linux)
import Glibc
#else
import Darwin.C
#endif
let url = "https://www.google.com"
if let url = URL(string: url) {
else { print("error") }
The text was updated successfully, but these errors were encountered: