Merge pull request #1804 from famedly/karthi/max-pdu-size-check

perf: check event size in bytes without encoding twice
This commit is contained in:
Krille-chan 2024-05-20 16:46:39 +02:00 committed by GitHub
commit fe637e1627
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 18 additions and 11 deletions

View File

@ -22,6 +22,10 @@ class Api {
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
/// additional keys, which MUST follow the Java package naming convention,
/// e.g. `com.example.myapp.property`. This ensures property names are
@ -3798,6 +3802,10 @@ class Api {
request.headers['authorization'] = 'Bearer ${bearerToken!}';
request.headers['content-type'] = 'application/json';
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 responseBody = await response.stream.toBytes();
if (response.statusCode != 200) unexpectedResponse(response, responseBody);

View File

@ -56,6 +56,11 @@ class MatrixApi extends Api {
super.unexpectedResponse(response, body);
}
@override
Never bodySizeExceeded(int expected, int actual) {
throw EventTooLarge(expected, actual);
}
MatrixApi({
Uri? homeserver,
String? accessToken,
@ -236,3 +241,8 @@ class MatrixApi extends Api {
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)
: content;
final utf8EncodedJsonLength =
utf8.encode(jsonEncode(sendMessageContent)).length;
if (utf8EncodedJsonLength > maxPDUSize) {
throw EventTooLarge(utf8EncodedJsonLength);
}
return await client.sendMessage(
id,
sendMessageContent.containsKey('ciphertext')
@ -2380,8 +2374,3 @@ enum EncryptionHealthState {
allVerified,
unverifiedDevices,
}
class EventTooLarge implements Exception {
int length;
EventTooLarge(this.length);
}