Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

159 lines
3.3 KiB

  1. package URI::WithBase;
  2. use strict;
  3. use vars qw($AUTOLOAD);
  4. use URI;
  5. use overload '""' => "as_string", fallback => 1;
  6. sub as_string; # help overload find it
  7. sub new
  8. {
  9. my($class, $uri, $base) = @_;
  10. my $ibase = $base;
  11. if ($base && ref($base) && UNIVERSAL::isa($base, "URI::WithBase")) {
  12. $base = $base->abs;
  13. $ibase = $base->[0];
  14. }
  15. bless [URI->new($uri, $ibase), $base], $class;
  16. }
  17. sub new_abs
  18. {
  19. my $class = shift;
  20. my $self = $class->new(@_);
  21. $self->abs;
  22. }
  23. sub _init
  24. {
  25. my $class = shift;
  26. my($str, $scheme) = @_;
  27. bless [URI->new($str, $scheme), undef], $class;
  28. }
  29. sub eq
  30. {
  31. my($self, $other) = @_;
  32. $other = $other->[0] if UNIVERSAL::isa($other, "URI::WithBase");
  33. $self->[0]->eq($other);
  34. }
  35. sub AUTOLOAD
  36. {
  37. my $self = shift;
  38. my $method = substr($AUTOLOAD, rindex($AUTOLOAD, '::')+2);
  39. return if $method eq "DESTROY";
  40. $self->[0]->$method(@_);
  41. }
  42. sub can { # override UNIVERSAL::can
  43. my $self = shift;
  44. $self->SUPER::can(@_) || (
  45. ref($self)
  46. ? $self->[0]->can(@_)
  47. : undef
  48. )
  49. }
  50. sub base {
  51. my $self = shift;
  52. my $base = $self->[1];
  53. if (@_) { # set
  54. my $new_base = shift;
  55. $new_base = $new_base->abs if ref($new_base); # ensure absoluteness
  56. $self->[1] = $new_base;
  57. }
  58. return unless defined wantarray;
  59. # The base attribute supports 'lazy' conversion from URL strings
  60. # to URL objects. Strings may be stored but when a string is
  61. # fetched it will automatically be converted to a URL object.
  62. # The main benefit is to make it much cheaper to say:
  63. # URI::WithBase->new($random_url_string, 'http:')
  64. if (defined($base) && !ref($base)) {
  65. $base = URI->new($base);
  66. $self->[1] = $base unless @_;
  67. }
  68. $base;
  69. }
  70. sub clone
  71. {
  72. my $self = shift;
  73. my $base = $self->[1];
  74. $base = $base->clone if ref($base);
  75. bless [$self->[0]->clone, $base], ref($self);
  76. }
  77. sub abs
  78. {
  79. my $self = shift;
  80. my $base = shift || $self->base || return $self->clone;
  81. bless [$self->[0]->abs($base, @_), $base], ref($self);
  82. }
  83. sub rel
  84. {
  85. my $self = shift;
  86. my $base = shift || $self->base || return $self->clone;
  87. bless [$self->[0]->rel($base, @_), $base], ref($self);
  88. }
  89. 1;
  90. __END__
  91. =head1 NAME
  92. URI::WithBase - URI which remember their base
  93. =head1 SYNOPSIS
  94. $u1 = URI::WithBase->new($str, $base);
  95. $u2 = $u1->abs;
  96. $base = $u1->base;
  97. $u1->base( $new_base )
  98. =head1 DESCRIPTION
  99. This module provide the C<URI::WithBase> class. Objects of this class
  100. are like C<URI> objects, but can keep their base too.
  101. The methods provided in addition to or modified from those of C<URI> are:
  102. =over 4
  103. =item $uri = URI::WithBase->new($str, [$base])
  104. The constructor takes a an optional base URI as the second argument.
  105. =item $uri->base( [$new_base] )
  106. This method can be used to get or set the value of the base attribute.
  107. =item $uri->abs( [$base_uri] )
  108. The $base_uri argument is now made optional as the object carries it's
  109. base with it.
  110. =item $uri->rel( [$base_uri] )
  111. The $base_uri argument is now made optional as the object carries it's
  112. base with it.
  113. =back
  114. =head1 SEE ALSO
  115. L<URI>
  116. =head1 COPYRIGHT
  117. Copyright 1998-2000 Gisle Aas.
  118. =cut