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.

139 lines
3.4 KiB

  1. package URI::data; # RFC 2397
  2. require URI;
  3. @ISA=qw(URI);
  4. use strict;
  5. use MIME::Base64 qw(encode_base64 decode_base64);
  6. use URI::Escape qw(uri_unescape);
  7. sub media_type
  8. {
  9. my $self = shift;
  10. my $opaque = $self->opaque;
  11. $opaque =~ /^([^,]*),?/ or die;
  12. my $old = $1;
  13. my $base64;
  14. $base64 = $1 if $old =~ s/(;base64)$//i;
  15. if (@_) {
  16. my $new = shift;
  17. $new = "" unless defined $new;
  18. $new =~ s/%/%25/g;
  19. $new =~ s/,/%2C/g;
  20. $base64 = "" unless defined $base64;
  21. $opaque =~ s/^[^,]*,?/$new$base64,/;
  22. $self->opaque($opaque);
  23. }
  24. return uri_unescape($old) if $old; # media_type can't really be "0"
  25. "text/plain;charset=US-ASCII"; # default type
  26. }
  27. sub data
  28. {
  29. my $self = shift;
  30. my($enc, $data) = split(",", $self->opaque, 2);
  31. unless (defined $data) {
  32. $data = "";
  33. $enc = "" unless defined $enc;
  34. }
  35. my $base64 = ($enc =~ /;base64$/i);
  36. if (@_) {
  37. $enc =~ s/;base64$//i if $base64;
  38. my $new = shift;
  39. $new = "" unless defined $new;
  40. my $uric_count = _uric_count($new);
  41. my $urienc_len = $uric_count + (length($new) - $uric_count) * 3;
  42. my $base64_len = int((length($new)+2) / 3) * 4;
  43. $base64_len += 7; # because of ";base64" marker
  44. if ($base64_len < $urienc_len || $_[0]) {
  45. $enc .= ";base64";
  46. $new = encode_base64($new, "");
  47. } else {
  48. $new =~ s/%/%25/g;
  49. }
  50. $self->opaque("$enc,$new");
  51. }
  52. return unless defined wantarray;
  53. return $base64 ? decode_base64($data) : uri_unescape($data);
  54. }
  55. # I could not find a better way to interpolate the tr/// chars from
  56. # a variable.
  57. my $ENC = $URI::uric;
  58. $ENC =~ s/%//;
  59. eval <<EOT; die $@ if $@;
  60. sub _uric_count
  61. {
  62. \$_[0] =~ tr/$ENC//;
  63. }
  64. EOT
  65. 1;
  66. __END__
  67. =head1 NAME
  68. URI::data - URI that contain immediate data
  69. =head1 SYNOPSIS
  70. use URI;
  71. $u = URI->new("data:");
  72. $u->media_type("image/gif");
  73. $u->data(scalar(`cat camel.gif`));
  74. print "$u\n";
  75. open(XV, "|xv -") and print XV $u->data;
  76. =head1 DESCRIPTION
  77. The C<URI::data> class supports C<URI> objects belonging to the I<data>
  78. URI scheme. The I<data> URI scheme is specified in RFC 2397. It
  79. allows inclusion of small data items as "immediate" data, as if it had
  80. been included externally. Examples:
  81. data:,Perl%20is%20good
  82. data:image/gif;base64,R0lGODdhIAAgAIAAAAAAAPj8+CwAAAAAI
  83. AAgAAAClYyPqcu9AJyCjtIKc5w5xP14xgeO2tlY3nWcajmZZdeJcG
  84. Kxrmimms1KMTa1Wg8UROx4MNUq1HrycMjHT9b6xKxaFLM6VRKzI+p
  85. KS9XtXpcbdun6uWVxJXA8pNPkdkkxhxc21LZHFOgD2KMoQXa2KMWI
  86. JtnE2KizVUkYJVZZ1nczBxXlFopZBtoJ2diXGdNUymmJdFMAADs=
  87. C<URI> objects belonging to the data scheme support the common methods
  88. (described in L<URI>) and the following two scheme specific methods:
  89. =over 4
  90. =item $uri->media_type( [$new_media_type] )
  91. This method can be used to get or set the media type specified in the
  92. URI. If no media type is specified, then the default
  93. C<"text/plain;charset=US-ASCII"> is returned.
  94. =item $uri->data( [$new_data] )
  95. This method can be used to get or set the data contained in the URI.
  96. The data is passed unescaped (in binary form). The decision about
  97. whether to base64 encode the data in the URI is taken automatically
  98. based on what encoding produces the shortest URI string.
  99. =back
  100. =head1 SEE ALSO
  101. L<URI>
  102. =head1 COPYRIGHT
  103. Copyright 1995-1998 Gisle Aas.
  104. This library is free software; you can redistribute it and/or
  105. modify it under the same terms as Perl itself.
  106. =cut