URL.pm 2.9 (was Re: a patch for URI::URL 2.7 (?) )

Martijn Koster (m.koster@nexor.co.uk)
Wed, 10 May 1995 10:43:29 +0100


In message <9505091320.aj27689@post.demon.co.uk>, Tim Bunce writes:

> > A fundamental problem I could not solve though: if the base url is a
> > directory, but has no trailing "/", then the path for the child url is
> > computed wrong. Is there a way to determine, if an url is a directory
> > in the absence of a trailing slash?
>
> That's an interesting question.

Nope. A server might know, and some cleverly redirect to the right
URL.  It'd be nice of HTTP had a facility for returning a document
along with a different 'better' URL to prevent the round trip. You
could do this with a new Response header for 200, or some new 300
trick. Slightly prone to abuse, but clients can guard against that.
I'm not aware of this being proposed for HTTP/1.1.

> ! sub port {
> !     my($self, $port) = @_;
> !     if (@_ > 1) {     # set
> !       # if port is default then unset it (simplifies comparisons)
> !       $port = undef if ($port and $port == $self->default_port);
> !       return $self->elem('port', @_);
> !     }
> !     # get, return default if unset
> 
> Could someone (Martijn?) on libwww-perl integrate this?

Ehr, no, it breaks the tests and doesn't work, I assume you mean
$port :-) I've alse added some tests to illustrate behaviour.

*** URL-2.8.pm	Fri Apr 28 11:16:04 1995
--- URL.pm	Wed May 10 10:28:14 1995
***************
*** 1,6 ****
  #!/usr/local/bin/perl -w
  #
! # $Id: URL.pm,v 2.8 1995/04/28 10:16:12 mak Exp $'
  #
  package URI::URL;
  require 5.001;
--- 1,6 ----
  #!/usr/local/bin/perl -w
  #
! # $Id: URL.pm,v 2.9 1995/05/10 09:28:42 mak Exp $'
  #
  package URI::URL;
  require 5.001;
***************
*** 290,296 ****
  @EXPORT_OK = qw(uri_escape uri_unescape);
  
  # Make the version number available
! ($Version) = '$Revision: 2.8 $' =~ /(\d+\.\d+)/;
  $Version += 0;  # shut up -w
  
  # Define default unsafe characters.
--- 290,296 ----
  @EXPORT_OK = qw(uri_escape uri_unescape);
  
  # Make the version number available
! ($Version) = '$Revision: 2.9 $' =~ /(\d+\.\d+)/;
  $Version += 0;  # shut up -w
  
  # Define default unsafe characters.
***************
*** 544,550 ****
  sub user     { shift->elem('user',    @_); }
  sub password { shift->elem('password',@_); }
  sub host     { shift->elem('host',    @_); }
! sub port     { shift->elem('port',    @_); }
  
  # optimisation to speed up elem() below:
  my %netloc_fields = qw(user 1 password 1 host 1 port 1);
--- 544,560 ----
  sub user     { shift->elem('user',    @_); }
  sub password { shift->elem('password',@_); }
  sub host     { shift->elem('host',    @_); }
! sub port {
!     my($self, $port) = @_;
!     if (@_ > 1) {     # set
!       # if port is default then unset it (simplifies comparisons)
!       $port = undef if ($port and $port == $self->default_port);
!       return $self->elem('port', $port);
!     }
!     # get, return default if unset
!     $self->elem('port') || $self->default_port;
! }
! 
  
  # optimisation to speed up elem() below:
  my %netloc_fields = qw(user 1 password 1 host 1 port 1);
***************
*** 1250,1256 ****
      $url->_expect('as_string', 'http://web/1info?key+words#this');
  
      &netloc_test;
! 
      $url->query(undef);
      $url->_expect('query', undef);
      $url->print_on;
--- 1260,1267 ----
      $url->_expect('as_string', 'http://web/1info?key+words#this');
  
      &netloc_test;
!     &port_test;
! 		  
      $url->query(undef);
      $url->_expect('query', undef);
      $url->print_on;
***************
*** 1283,1288 ****
--- 1294,1336 ----
      $url->_expect('netloc', 'hst2');
  }
  
+ #
+ # port_test()
+ #
+ # Test port behaviour
+ #
+ sub port_test {
+     print "port_test:\n";
+ 
+     $url = URI::URL->new('http://foo/root/dir/');
+     my $port = $url->port;
+     die "Port undefined" unless defined $port;
+     die "Wrong port $port" unless $port == 80;
+     die "Wrong string" unless $url->as_string eq
+ 	'http://foo/root/dir/';
+ 
+     $url->port(8001);
+     $port = $url->port;
+     die "Port undefined" unless defined $port;
+     die "Wrong port $port" unless $port == 8001;
+     die "Wrong string" unless $url->as_string eq 
+ 	'http://foo:8001/root/dir/';
+ 
+     $url->port(80);
+     $port = $url->port;
+     die "Port undefined" unless defined $port;
+     die "Wrong port $port" unless $port == 80;
+     die "Wrong string" unless $url->as_string eq 
+ 	'http://foo/root/dir/';
+ 
+     $url->port(8001);
+     $url->port(undef);
+     $port = $url->port;
+     die "Port undefined" unless defined $port;
+     die "Wrong port $port" unless $port == 80;
+     die "Wrong string" unless $url->as_string eq 
+ 	'http://foo/root/dir/';
+ }
  
  
  #####################################################################

-- Martijn
__________
Internet: m.koster@nexor.co.uk
X-400: C=GB; A= ; P=Nexor; O=Nexor; S=koster; I=M
WWW: http://web.nexor.co.uk/mak/mak.html