Merge pull request #5394 from 21pages/ab

opt ab json decode
This commit is contained in:
RustDesk 2023-08-15 12:11:39 +08:00 committed by GitHub
commit a1215749d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -78,15 +78,8 @@ class AbModel {
tags.clear(); tags.clear();
peers.clear(); peers.clear();
} else if (resp.body.isNotEmpty) { } else if (resp.body.isNotEmpty) {
Map<String, dynamic> json; Map<String, dynamic> json =
try { _jsonDecode(utf8.decode(resp.bodyBytes), resp.statusCode);
json = jsonDecode(utf8.decode(resp.bodyBytes));
} catch (e) {
if (resp.statusCode != 200) {
throw 'HTTP ${resp.statusCode}, $e';
}
rethrow;
}
if (json.containsKey('error')) { if (json.containsKey('error')) {
throw json['error']; throw json['error'];
} else if (json.containsKey('data')) { } else if (json.containsKey('data')) {
@ -221,7 +214,7 @@ class AbModel {
}); });
http.Response resp; http.Response resp;
// support compression // support compression
if (licensedDevices > 0 && body.length > 1024) { if (licensedDevices > 0 && body.length > 102400) {
authHeaders['Content-Encoding'] = "gzip"; authHeaders['Content-Encoding'] = "gzip";
resp = await http.post(Uri.parse(api), resp = await http.post(Uri.parse(api),
headers: authHeaders, body: GZipCodec().encode(utf8.encode(body))); headers: authHeaders, body: GZipCodec().encode(utf8.encode(body)));
@ -229,25 +222,18 @@ class AbModel {
resp = resp =
await http.post(Uri.parse(api), headers: authHeaders, body: body); await http.post(Uri.parse(api), headers: authHeaders, body: body);
} }
try { if (resp.statusCode == 200 &&
if (resp.statusCode == 200 && (resp.body.isEmpty || resp.body.toLowerCase() == 'null')) {
(resp.body.isEmpty || resp.body.toLowerCase() == 'null')) { _saveCache();
} else {
Map<String, dynamic> json = _jsonDecode(resp.body, resp.statusCode);
if (json.containsKey('error')) {
throw json['error'];
} else if (resp.statusCode == 200) {
_saveCache(); _saveCache();
} else { } else {
final json = jsonDecode(resp.body); throw 'HTTP ${resp.statusCode}';
if (json.containsKey('error')) {
throw json['error'];
} else if (resp.statusCode == 200) {
_saveCache();
} else {
throw 'HTTP ${resp.statusCode}';
}
} }
} catch (e) {
if (resp.statusCode != 200) {
throw 'HTTP ${resp.statusCode}, $e';
}
rethrow;
} }
} catch (e) { } catch (e) {
pushError.value = pushError.value =
@ -469,4 +455,17 @@ class AbModel {
debugPrint("load ab cache: $e"); debugPrint("load ab cache: $e");
} }
} }
Map<String, dynamic> _jsonDecode(String body, int statusCode) {
try {
Map<String, dynamic> json = jsonDecode(body);
return json;
} catch (e) {
final err = body.isNotEmpty && body.length < 128 ? body : e.toString();
if (statusCode != 200) {
throw 'HTTP $statusCode, $err';
}
throw err;
}
}
} }