Skip to content
This repository has been archived by the owner on Feb 9, 2024. It is now read-only.

Commit

Permalink
Decompress response bodies
Browse files Browse the repository at this point in the history
  • Loading branch information
MickeMakaron committed Sep 13, 2022
1 parent b883a23 commit 7bc168b
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions gotm/_impl/_GotmUtility.gd
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,30 @@ static func fetch_data(url: String, method: int = HTTPClient.METHOD_GET, body =
free_clients.append(client)
if !has_yielded:
yield(get_tree(), "idle_frame")
if OS.get_name () != "HTML5" && response.headers.get("content-encoding") == "gzip":
response.data = decompress_gzip(response.data)
return response


## Only supported in 3.4.0 and later.
static func suppress_error_messages(suppress: bool) -> void:
Engine.set("print_error_messages", !suppress)

static func decompress_gzip(data: PoolByteArray) -> PoolByteArray:
if data.empty():
return PoolByteArray()
var decompressed_size: int = data.size() * 2
for i in range(0, 8):
suppress_error_messages(true)
var decompressed := data.decompress(decompressed_size, File.COMPRESSION_GZIP)
suppress_error_messages(false)
if !decompressed.empty():
return decompressed
decompressed_size *= 2

return PoolByteArray()


static func fetch_json(url: String, method: int = HTTPClient.METHOD_GET, body = null, headers: PoolStringArray = []) -> FetchDataResult:
var result = yield(fetch_data(url, method, body, headers), "completed")
var data_string = result.data.get_string_from_utf8()
Expand Down

0 comments on commit 7bc168b

Please sign in to comment.