From 9f6bd740cacb5038f6b69d5645e6feb0e5ff737d Mon Sep 17 00:00:00 2001 From: Marcel Date: Mon, 20 Jul 2020 13:19:57 +0200 Subject: [PATCH 1/2] [Requests] Make sure to not use the latin1 version of the json but instead convert the bytes to utf8 as required by various RFCs. Took 14 minutes --- lib/matrix_api/matrix_api.dart | 10 ++++++---- lib/src/client.dart | 9 +++++---- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/lib/matrix_api/matrix_api.dart b/lib/matrix_api/matrix_api.dart index 6f87e88..064417a 100644 --- a/lib/matrix_api/matrix_api.dart +++ b/lib/matrix_api/matrix_api.dart @@ -18,13 +18,14 @@ import 'dart:async'; import 'dart:convert'; + import 'package:famedlysdk/matrix_api/model/filter.dart'; import 'package:famedlysdk/matrix_api/model/keys_query_response.dart'; import 'package:famedlysdk/matrix_api/model/login_types.dart'; import 'package:famedlysdk/matrix_api/model/notifications_query_response.dart'; import 'package:famedlysdk/matrix_api/model/open_graph_data.dart'; -import 'package:famedlysdk/matrix_api/model/request_token_response.dart'; import 'package:famedlysdk/matrix_api/model/profile.dart'; +import 'package:famedlysdk/matrix_api/model/request_token_response.dart'; import 'package:famedlysdk/matrix_api/model/server_capabilities.dart'; import 'package:famedlysdk/matrix_api/model/supported_versions.dart'; import 'package:famedlysdk/matrix_api/model/sync_update.dart'; @@ -36,12 +37,12 @@ import 'package:mime/mime.dart'; import 'package:moor/moor.dart'; import 'model/device.dart'; -import 'model/matrix_event.dart'; -import 'model/matrix_keys.dart'; import 'model/event_context.dart'; import 'model/events_sync_update.dart'; import 'model/login_response.dart'; +import 'model/matrix_event.dart'; import 'model/matrix_exception.dart'; +import 'model/matrix_keys.dart'; import 'model/one_time_keys_claim_response.dart'; import 'model/open_id_credentials.dart'; import 'model/presence_content.dart'; @@ -192,7 +193,8 @@ class MatrixApi { ); break; } - var jsonString = String.fromCharCodes(resp.body.runes); + var resp_body = utf8.decode(resp.bodyBytes); + var jsonString = String.fromCharCodes(resp_body.runes); if (jsonString.startsWith('[') && jsonString.endsWith(']')) { jsonString = '\{"chunk":$jsonString\}'; } diff --git a/lib/src/client.dart b/lib/src/client.dart index 8815688..3d552f5 100644 --- a/lib/src/client.dart +++ b/lib/src/client.dart @@ -20,9 +20,9 @@ import 'dart:async'; import 'dart:convert'; import 'dart:core'; +import 'package:famedlysdk/encryption.dart'; import 'package:famedlysdk/famedlysdk.dart'; import 'package:famedlysdk/matrix_api.dart'; -import 'package:famedlysdk/encryption.dart'; import 'package:famedlysdk/src/room.dart'; import 'package:famedlysdk/src/utils/device_keys_list.dart'; import 'package:famedlysdk/src/utils/matrix_file.dart'; @@ -30,12 +30,12 @@ import 'package:famedlysdk/src/utils/to_device_event.dart'; import 'package:http/http.dart' as http; import 'package:pedantic/pedantic.dart'; +import 'database/database.dart' show Database; import 'event.dart'; import 'room.dart'; +import 'user.dart'; import 'utils/event_update.dart'; import 'utils/room_update.dart'; -import 'user.dart'; -import 'database/database.dart' show Database; typedef RoomSorter = int Function(Room a, Room b); @@ -231,7 +231,8 @@ class Client { ) async { final response = await http .get('https://${MatrixIdOrDomain.domain}/.well-known/matrix/client'); - final rawJson = json.decode(response.body); + var resp_body = utf8.decode(response.bodyBytes); + final rawJson = json.decode(resp_body); return WellKnownInformations.fromJson(rawJson); } From 3c0fbf784f6f9376f3830fb821ceeb99f664f988 Mon Sep 17 00:00:00 2001 From: Marcel Date: Mon, 20 Jul 2020 13:43:55 +0200 Subject: [PATCH 2/2] [Requests] Add try_catch for utf8 decoding because dart really seems to have issues with json encoding and decoding. (also use camelCase) Took 18 minutes --- lib/matrix_api/matrix_api.dart | 9 +++++++-- lib/src/client.dart | 9 +++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/lib/matrix_api/matrix_api.dart b/lib/matrix_api/matrix_api.dart index 064417a..770dd25 100644 --- a/lib/matrix_api/matrix_api.dart +++ b/lib/matrix_api/matrix_api.dart @@ -193,8 +193,13 @@ class MatrixApi { ); break; } - var resp_body = utf8.decode(resp.bodyBytes); - var jsonString = String.fromCharCodes(resp_body.runes); + var respBody = resp.body; + try { + respBody = utf8.decode(resp.bodyBytes); + } catch (_) { + // No-OP + } + var jsonString = String.fromCharCodes(respBody.runes); if (jsonString.startsWith('[') && jsonString.endsWith(']')) { jsonString = '\{"chunk":$jsonString\}'; } diff --git a/lib/src/client.dart b/lib/src/client.dart index 3d552f5..bdd2ada 100644 --- a/lib/src/client.dart +++ b/lib/src/client.dart @@ -231,8 +231,13 @@ class Client { ) async { final response = await http .get('https://${MatrixIdOrDomain.domain}/.well-known/matrix/client'); - var resp_body = utf8.decode(response.bodyBytes); - final rawJson = json.decode(resp_body); + var respBody = response.body; + try { + respBody = utf8.decode(response.bodyBytes); + } catch (_) { + // No-OP + } + final rawJson = json.decode(respBody); return WellKnownInformations.fromJson(rawJson); }