Skip to content
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

file name with leading and/or trailing space #95

Open
jackdeguest opened this issue Apr 20, 2022 · 2 comments
Open

file name with leading and/or trailing space #95

jackdeguest opened this issue Apr 20, 2022 · 2 comments
Labels

Comments

@jackdeguest
Copy link

In URI version 5.10, if a file name is provided that has leading and or trailing spaces, those will be removed since URI::file::Base::new() calls URI->new to generate the object, and URI explicitly states it removes leading and trailing spaces.

See code below as an example:

#!/usr/local/bin/perl
use URI::file;
use File::Spec;
use v5.16;

my $file = ' some file ';
my $u = URI::file->new_abs( $file );
say "file path is: '$u'";
my @dirs = File::Spec->splitdir( $u->file( 'Unix' ) );
say $dirs[-1] eq $file ? 'ok' : ( "not ok: '" . $dirs[-1] . "', expected '$file'" );
exit(0);

__END__

This will yield something like:

file path is: 'file:///home/joe/some%20file'
not ok: 'some file', expected ' some file '

whereas, it should return: file:///home/joe/%20some%20file%20

and $u->file should return ' some file '

The mitigation is to set back the path after the object has been instantiated, such as:

use URI::file;
use Cwd;
my $file = ' some file ';
my $u2 = URI::file->new( $file );
$u2->path( $file );
$u2 = $u2->abs( Cwd::cwd );
say "'" . $u2->file( 'Unix' ) . "'";

which would correctly yield '/home/joe/ some file '

@oalders
Copy link
Member

oalders commented Apr 21, 2022

Looks like the code in question is at https://metacpan.org/release/OALDERS/URI-5.10/source/lib/URI.pm#L43-44

@oalders oalders added the bug label Apr 21, 2022
@jackdeguest
Copy link
Author

Looks like the code in question is at https://metacpan.org/release/OALDERS/URI-5.10/source/lib/URI.pm#L43-44

Yes, exactly what I was referring to. I can understand the need to remove leading and trailing space for web URI, but for file URI, I doubt it. Wikipedia page seems to indicate they should not, at least on Windows.

The section on file URI syntax of the original rfc3986 indicates the segments should contain percent-encoded characters to represent the spaces: "A percent-encoding mechanism is used to represent a data octet in a component when that octet's corresponding character is outside the allowed set or is being used as a delimiter of, or within, the component"[1]

I am going to test this and issue a pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants