todo-app/lib/mcp/request_guard.dart
Krzysztof kuhy Rudnicki b350b3e8c1
Some checks are pending
dependency freshness / dependencies on newest stable (push) Waiting to run
file-length / file length <= 250 lines (push) Waiting to run
md-naming / markdown naming convention (push) Waiting to run
release-apk / release (APK) (push) Waiting to run
repo-contract / repo contract (push) Waiting to run
Tidy MCP deadline guard for analyzer
Guard moves to request_guard.dart (file-length gate); README notes
the token reload, deadline and timeouts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WzxdWXgrX1QxVjqMiz6KFs
2026-11-16 08:06:05 +01:00

41 lines
1.2 KiB
Dart

import 'dart:async';
import 'dart:io';
/// Answers 503 here because the library can only say 403 for a refusal; it
/// then fails to write its own 403 ("Header already sent", swallowed by the
/// zone guard in `TodoMcpServer.start`) and the request stays refused.
bool answerUnavailable(HttpRequest request) {
request.response
..statusCode = HttpStatus.serviceUnavailable
..write('Service Unavailable')
..close().ignore();
return false;
}
/// Closes [request]'s response with 503 once [deadline] passes, unless it was
/// answered first. Safe on any state: a response already closed or a dead
/// socket is ignored.
void guardDeadline(HttpRequest request, Duration deadline) {
if (request.method == 'GET') return;
final response = request.response;
final timer = Timer(deadline, () async {
try {
response
..statusCode = HttpStatus.serviceUnavailable
..write('Request timed out');
} on Object {
// Headers already sent: just end the stream.
}
try {
await response.close();
} on Object {
// The peer is gone; the socket is released either way.
}
});
unawaited(
response.done
.then<void>((_) {}, onError: (_) {})
.whenComplete(timer.cancel),
);
}