perf: check event size in bytes without encoding twice

This commit is contained in:
Karthikeyan S 2024-05-15 13:04:52 +05:30 committed by krille-chan
parent f9fe6b17a9
commit 29e554337c
No known key found for this signature in database
3 changed files with 18 additions and 11 deletions

View File

@ -22,6 +22,10 @@ class Api {
throw Exception('http error response'); throw Exception('http error response');
} }
Never bodySizeExceeded(int expected, int actual) {
throw Exception('body size $actual exceeded $expected');
}
/// Gets discovery information about the domain. The file may include /// Gets discovery information about the domain. The file may include
/// additional keys, which MUST follow the Java package naming convention, /// additional keys, which MUST follow the Java package naming convention,
/// e.g. `com.example.myapp.property`. This ensures property names are /// e.g. `com.example.myapp.property`. This ensures property names are
@ -3798,6 +3802,10 @@ class Api {
request.headers['authorization'] = 'Bearer ${bearerToken!}'; request.headers['authorization'] = 'Bearer ${bearerToken!}';
request.headers['content-type'] = 'application/json'; request.headers['content-type'] = 'application/json';
request.bodyBytes = utf8.encode(jsonEncode(body)); request.bodyBytes = utf8.encode(jsonEncode(body));
const maxBodySize = 60000;
if (request.bodyBytes.length > maxBodySize) {
bodySizeExceeded(maxBodySize, request.bodyBytes.length);
}
final response = await httpClient.send(request); final response = await httpClient.send(request);
final responseBody = await response.stream.toBytes(); final responseBody = await response.stream.toBytes();
if (response.statusCode != 200) unexpectedResponse(response, responseBody); if (response.statusCode != 200) unexpectedResponse(response, responseBody);

View File

@ -56,6 +56,11 @@ class MatrixApi extends Api {
super.unexpectedResponse(response, body); super.unexpectedResponse(response, body);
} }
@override
Never bodySizeExceeded(int expected, int actual) {
throw EventTooLarge(expected, actual);
}
MatrixApi({ MatrixApi({
Uri? homeserver, Uri? homeserver,
String? accessToken, String? accessToken,
@ -236,3 +241,8 @@ class MatrixApi extends Api {
return getRoomKeyBySessionId(roomId, sessionId, version); return getRoomKeyBySessionId(roomId, sessionId, version);
} }
} }
class EventTooLarge implements Exception {
int maxSize, actualSize;
EventTooLarge(this.maxSize, this.actualSize);
}

View File

@ -966,12 +966,6 @@ class Room {
.encryptGroupMessagePayload(id, content, type: type) .encryptGroupMessagePayload(id, content, type: type)
: content; : content;
final utf8EncodedJsonLength =
utf8.encode(jsonEncode(sendMessageContent)).length;
if (utf8EncodedJsonLength > maxPDUSize) {
throw EventTooLarge(utf8EncodedJsonLength);
}
return await client.sendMessage( return await client.sendMessage(
id, id,
sendMessageContent.containsKey('ciphertext') sendMessageContent.containsKey('ciphertext')
@ -2380,8 +2374,3 @@ enum EncryptionHealthState {
allVerified, allVerified,
unverifiedDevices, unverifiedDevices,
} }
class EventTooLarge implements Exception {
int length;
EventTooLarge(this.length);
}