Skip to content

Abort or cancel multipart request #567

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
intoxicated opened this issue May 4, 2021 · 2 comments · May be fixed by #978
Closed

Abort or cancel multipart request #567

intoxicated opened this issue May 4, 2021 · 2 comments · May be fixed by #978
Labels
needs-info Additional information needed from the issue author

Comments

@intoxicated
Copy link

It seems like neither abort or cancel method are provided from http package, though there is abort in dart:io package for httpClientRequest.

Is it safe to call close on client for multipart request?

class MultipartRequest extends http.MultipartRequest {
  /// Creates a new [MultipartRequest].
  var client = http.Client();

  MultipartRequest(
    String method,
    Uri url, {
    this.onProgress,
  }) : super(method, url);

  final void Function(int bytes, int totalBytes) onProgress;

  void cancel() => client.close();

  @override
  Future<http.StreamedResponse> send() async {
    try {
      var response = await client.send(this);
      var stream = onDone(response.stream, client.close);
      return http.StreamedResponse(
        http.ByteStream(stream),
        response.statusCode,
        contentLength: response.contentLength,
        request: response.request,
        headers: response.headers,
        isRedirect: response.isRedirect,
        persistentConnection: response.persistentConnection,
        reasonPhrase: response.reasonPhrase,
      );
    } catch (_) {
      client.close();
      rethrow;
    }
  }

  Stream<T> onDone<T>(Stream<T> stream, void Function() onDone) =>
      stream.transform(StreamTransformer.fromHandlers(handleDone: (sink) {
        sink.close();
        onDone();
      }));

  /// Freezes all mutable fields and returns a single-subscription [ByteStream]
  /// that will emit the request body.
  @override
  http.ByteStream finalize() {
    final byteStream = super.finalize();
    if (onProgress == null) return byteStream;

    final total = contentLength;
    var bytes = 0;

    final t = StreamTransformer.fromHandlers(
      handleData: (List<int> data, EventSink<List<int>> sink) {
        bytes += data.length;
        if (onProgress != null) onProgress(bytes, total);
        sink.add(data);
      },
    );
    final stream = byteStream.transform(t);
    return http.ByteStream(stream);
  }
}

I saw some error were thrown (can't reproduce again but it was complaining about the connection was closed, it had not been reached content length the request claimed to fulfill).

@natebosch
Copy link
Member

Is it safe to call close on client for multipart request?

I think it should be.

it was complaining about the connection was closed, it had not been reached content length the request claimed to fulfill

Was that server side or client side?

It seems like neither abort or cancel method are provided from http package

Not currently. Work is tracked in #424

Copy link

github-actions bot commented Nov 1, 2024

Without additional information we're not able to resolve this issue. Feel free to add more info or respond to any questions above and we can reopen the case. Thanks for your contribution!

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Nov 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-info Additional information needed from the issue author
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants