mirror of
https://github.com/block/buzz.git
synced 2026-11-27 19:52:31 +00:00
Follow-ups from the review of #9811 (Android push enrollment): the review items that were safe to leave for after merge. - **A timed-out gateway call is now aborted.** The 15 s timeout used to abandon the POST while it kept running, so a retry could race the original request at the gateway. The gateway client now sends through `boundedHttpResponse`, which aborts the request at the deadline (and caps the reply size). - **An Android gateway reply the app can't read is reported to diagnostics,** as #9806 does for iOS. Malformed replies (no JSON object, missing fields, or an oversized reply with the expected status) throw `AndroidPushGatewayResponseInvalid`, which `enrollBuzzPush` reports under the push gateway operation. Refusals and transport failures stay unreported, and a refusal keeps its status even when its body is over the cap. - **The delegation sweep after enrollment keeps the enrolling relay while it is still wanted.** It now sweeps with the desired relays plus the enrolling origin, but only while `stillWanted()` holds, so an opt-out that landed during the delegation still sees that delegation revoked. - **Tests for two paths that had none:** an opt-out that lands while the gateway answers the delegation ends the attempt quietly (no lease, delegation revoked, no failure retry), and an opted-out iOS community enrolls nothing. Two other items from the review, both Codex findings on an earlier head of #9811 (reusing a revoked grant after a lost re-delegation; deleting a key after a lost renewal), were already fixed and tested before #9811 merged, so nothing changes here for them. ### Validation Added tests: - [the enrolling relay keeps its delegation before the caller lists it](157d6d4759/mobile/test/shared/push/android_push_authority_test.dart): the sweep keeps a still-wanted relay that the desired list doesn't show yet, and revokes a delegation whose community opted out while it was in flight. - [turning push off while it delegates ends the attempt quietly](157d6d4759/mobile/test/features/settings/android_notifications_test.dart): production bootstrap, opt-out while the delegation response is held. - [only a gateway reply the app cannot read is reported](157d6d4759/mobile/test/shared/push/push_bridge_android_test.dart) and [a gateway call that times out is aborted, not abandoned](157d6d4759/mobile/test/shared/push/push_bridge_android_test.dart). - [a community that opted out enrolls nothing](157d6d4759/mobile/test/shared/push/push_bridge_test.dart) (iOS). - [an unavailable gateway keeps the journal and a refusal clears it](157d6d4759/mobile/test/shared/push/android_push_authority_test.dart): now also with refusal bodies over the 64 KiB cap. Each test was checked to fail with its guard removed. No device or full-app run: every change is on the Android push gateway path (which needs a gateway-configured Android build) or is a test, and the behavior changed is timeout, malformed-reply and race handling that the tests above drive directly against the production authority and bootstrap. ### Regressions - iOS is unchanged apart from the new test; the abort and diagnostics changes are Android-only code. - `boundedHttpResponse` gains an optional `timeout` that defaults to its old 30 s, so existing callers behave as before. - Gateway replies are capped at 64 KiB. Gateway responses are small JSON objects, so an oversized reply with the expected status is itself a defect and is reported. An oversized refusal is handled as that refusal (a 503 stays retryable, a 403 discards the pending enrollment). --------- Signed-off-by: Tom Brow <tomb@block.xyz> Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@localhost>
70 lines
2.1 KiB
Dart
70 lines
2.1 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:http/http.dart' as http;
|
|
|
|
import '../http/app_http_client.dart';
|
|
|
|
/// One deadline, [timeout], covers headers and the bounded body. Abort only
|
|
/// this request, leaving the shared client usable for sign-out and subsequent
|
|
/// attempts.
|
|
/// [onHeaders] sees the response as soon as its headers arrive, before the
|
|
/// body is read, even if reading the body then fails.
|
|
Future<http.Response> boundedHttpResponse(
|
|
http.Client client,
|
|
http.Request source, {
|
|
int maxBytes = 65536,
|
|
Duration timeout = const Duration(seconds: 30),
|
|
void Function(http.BaseResponse headers)? onHeaders,
|
|
}) async {
|
|
final abort = Completer<void>();
|
|
final request =
|
|
http.AbortableRequest(
|
|
source.method,
|
|
source.url,
|
|
abortTrigger: abort.future,
|
|
)
|
|
..followRedirects = false
|
|
..headers.addAll(source.headers)
|
|
..bodyBytes = source.bodyBytes;
|
|
StreamIterator<List<int>>? body;
|
|
Future<http.Response> read() async {
|
|
final response = await client.send(request);
|
|
try {
|
|
onHeaders?.call(response);
|
|
} catch (_) {
|
|
// Some clients hold the connection until the body is listened to and
|
|
// cancelled; an abort alone leaves it open.
|
|
unawaited(response.stream.listen(null).cancel());
|
|
rethrow;
|
|
}
|
|
final iterator = body = StreamIterator(response.stream);
|
|
if (abort.isCompleted) {
|
|
await iterator.cancel();
|
|
throw http.RequestAbortedException();
|
|
}
|
|
final bytes = <int>[];
|
|
while (await iterator.moveNext()) {
|
|
if (bytes.length + iterator.current.length > maxBytes) {
|
|
throw const FormatException('Response too large');
|
|
}
|
|
bytes.addAll(iterator.current);
|
|
}
|
|
return http.Response.bytes(
|
|
bytes,
|
|
response.statusCode,
|
|
headers: response.headers,
|
|
);
|
|
}
|
|
|
|
try {
|
|
return await read().timeout(timeout);
|
|
} on TimeoutException {
|
|
noteHttpDeadline(request);
|
|
rethrow;
|
|
} finally {
|
|
abort.complete();
|
|
// Do not let transport cleanup hold the account's serial queue hostage.
|
|
unawaited(body?.cancel());
|
|
}
|
|
}
|