mojo icon indicating copy to clipboard operation
mojo copied to clipboard

UA can't upload a Mojo::Asset::File having start_range & end_range

Open akarelas opened this issue 4 years ago • 2 comments

  • Mojolicious version: 9.22
  • Perl version: v5.34
  • Operating system: Ubuntu Linux 20.04

Steps to reproduce the behavior

Attempts to upload part of a file with Mojo::UserAgent, using Mojo::Asset::File and the asset's start_range/end_range properties fail after a long pause.

use Data::Dumper;

my $asset = Mojo::Asset::File->new(
    path        => 'file.txt',
    start_range => 100,
    end_range   => 105,
);

my $ua = Mojo::UserAgent->new;

my $tx = $ua->post('http://localhost:3000/upload_file', form => {
    up => { file => $asset },
});

print Dumper($tx->error);

Expected behavior

I expected those 6 bytes to be uploaded quickly and no error to occur.

Actual behavior

After 60 seconds I got this error:

$VAR1 = {
          'error' => {
                       'message' => 'Premature connection close'
                     }
        };

akarelas avatar Nov 07 '21 12:11 akarelas

Mojo::Asset::File->size() only ever returns the size of the whole asset so the multipart message has the wrong content length, hence the premature connection close as the server waits for more content. It can be done with a subset of the asset by creating a new one to pass in the form {file => Mojo::Asset::File->new->add_chunk($asset->get_chunk($start, $end))}

kiwiroy avatar Nov 08 '21 09:11 kiwiroy

That seems like a bug.

kraih avatar Nov 08 '21 10:11 kraih