@@ -222,6 +222,39 @@ extension MacOSDataFeed {
222
222
}
223
223
224
224
class SOFA : NSObject , URLSessionDelegate {
225
+ func decompressGzip( data: Data ) -> Data ? {
226
+ let tempDir = FileManager . default. temporaryDirectory
227
+ let compressedFileURL = tempDir. appendingPathComponent ( UUID ( ) . uuidString)
228
+ let decompressedFileURL = tempDir. appendingPathComponent ( UUID ( ) . uuidString)
229
+
230
+ do {
231
+ // Write compressed data to a temporary file
232
+ try data. write ( to: compressedFileURL)
233
+
234
+ // Use gzip to decompress the file
235
+ let result = SubProcessUtilities ( ) . runProcess ( launchPath: " /usr/bin/gzip " , arguments: [ " --decompress " , " --to-stdout " , compressedFileURL. path] )
236
+
237
+ // Clean up temporary files
238
+ try FileManager . default. removeItem ( at: compressedFileURL)
239
+
240
+ if result. exitCode != 0 {
241
+ LogManager . error ( " gzip failed with status \( result. exitCode) : \( result. error) " , logger: utilsLog)
242
+ return nil
243
+ }
244
+
245
+ // Convert the output string back to Data
246
+ guard let decompressedData = result. output. data ( using: . utf8) else {
247
+ LogManager . error ( " Failed to convert decompressed output to Data " , logger: utilsLog)
248
+ return nil
249
+ }
250
+
251
+ return decompressedData
252
+ } catch {
253
+ LogManager . error ( " Failed to decompress gzip data using command: \( error. localizedDescription) " , logger: utilsLog)
254
+ return nil
255
+ }
256
+ }
257
+
225
258
func URLSync( url: URL , maxRetries: Int = 3 ) -> ( data: Data ? , response: URLResponse ? , error: Error ? , responseCode: Int ? , eTag: String ? ) {
226
259
let semaphore = DispatchSemaphore ( value: 0 )
227
260
let lastEtag = Globals . nudgeDefaults. string ( forKey: " LastEtag " ) ?? " "
@@ -245,9 +278,10 @@ class SOFA: NSObject, URLSessionDelegate {
245
278
// Retry loop
246
279
while attempts < maxRetries {
247
280
attempts += 1
248
- let task = session. dataTask ( with: request) { data, resp, error in
281
+ let task = session. dataTask ( with: request) { [ weak self] data, resp, error in
282
+ guard let self = self else { return } // To handle self being nil
249
283
guard let httpResponse = resp as? HTTPURLResponse else {
250
- print ( " Error receiving response: \( error? . localizedDescription ?? " No error information " ) " )
284
+ LogManager . error ( " Error receiving response: \( error? . localizedDescription ?? " No error information " ) " , logger : utilsLog )
251
285
semaphore. signal ( )
252
286
return
253
287
}
@@ -258,11 +292,30 @@ class SOFA: NSObject, URLSessionDelegate {
258
292
eTag = etag
259
293
}
260
294
successfulQuery = true
295
+
296
+ if let encoding = httpResponse. allHeaderFields [ " Content-Encoding " ] as? String {
297
+ LogManager . debug ( " Content-Encoding: \( encoding) " , logger: utilsLog)
298
+
299
+ if encoding == " gzip " , let compressedData = data {
300
+ responseData = self . decompressGzip ( data: compressedData)
301
+
302
+ if responseData == nil {
303
+ LogManager . error ( " Failed to decompress gzip data " , logger: utilsLog)
304
+ responseData = data // Fall back to using the original data
305
+ } else {
306
+ LogManager . debug ( " Successfully decompressed gzip data " , logger: utilsLog)
307
+ }
308
+ } else {
309
+ responseData = data
310
+ }
311
+ } else {
312
+ responseData = data
313
+ }
314
+
261
315
} else if responseCode == 304 {
262
316
successfulQuery = true
263
317
}
264
318
265
- responseData = data
266
319
response = resp
267
320
responseError = error
268
321
semaphore. signal ( )
0 commit comments