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

Don't omit "/" if it has QUERY_STRING. #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

hiratara
Copy link
Contributor

RFC 1738 says, `If neither <path> nor <searchpart> is present, the "/" may also be omitted.'.

http://www.rfc-editor.org/rfc/rfc1738.txt

So we can't omit "/" if the URL has '?' and QUERY_STRINGs.

I thought that the following patch is better than this pull-req, but it makes t/old-base.t broken.

diff --git a/URI/http.pm b/URI/http.pm
index cb69822..8631e40 100644
--- a/URI/http.pm
+++ b/URI/http.pm
@@ -13,7 +13,7 @@ sub canonical
     my $other = $self->SUPER::canonical;

     my $slash_path = defined($other->authority) &&
-        !length($other->path) && !defined($other->query);
+        !length($other->path) && defined($other->query);

     if ($slash_path) {
        $other = $other->clone if $other == $self;
diff --git a/t/http.t b/t/http.t
index 2b8c44b..65e32cd 100644
--- a/t/http.t
+++ b/t/http.t
@@ -1,6 +1,6 @@
 #!perl -w

-print "1..15\n";
+print "1..17\n";

 use URI;

@@ -61,3 +61,12 @@ $u = URI->new("http://%77%77%77%2e%70%65%72%6c%2e%63%6f%6d/%7
 print "not " unless $u->canonical eq "http://www.perl.com/pub/a/2001/08/27/bjor
 print "ok 15\n";

+# RFC 1738: "/" can't be ommited when <searchpart> is present.
+$u = URI->new("http://localhost?p=1");
+print "not " unless $u->canonical eq "http://localhost/?p=1";
+print "ok 16\n";
+
+$u = URI->new("http://localhost");
+print "not " unless $u->canonical eq "http://localhost";
+print "ok 17\n";
+

We can also omit "/" when <path> and <searchpart> are empty,
but it makes t/old-base.t fail.
@gisle
Copy link
Member

gisle commented Feb 14, 2012

RFC 2396 does not agree on this AFAICT.

@hiratara
Copy link
Contributor Author

RFC3986 says that RFC2396 was obsolete. So we should follow RFC3986(and RFC1738), shouldn't we ??

http://www.ietf.org/rfc/rfc3986.txt

@gisle
Copy link
Member

gisle commented Mar 30, 2012

RFC3986:

URI           = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
hier-part     = "//" authority path-abempty
path-abempty  = *( "/" segment )

This does allow "http://localhost?p=1". Does compatiblity with other stuff break with this, or do you want to argue the aesthetics of of this as canonical form?

@gisle
Copy link
Member

gisle commented Mar 30, 2012

We still have this inconsistency:

$ perl -MURI -le 'print URI->new("http://foo#bar")->canonical'
http://foo/#bar
$ perl -MURI -le 'print URI->new("http://foo?bar")->canonical'
http://foo?bar

It's better to be consistent, so one of these ought to change.

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

Successfully merging this pull request may close these issues.

2 participants