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.

94 lines
2.4 KiB

  1. package URI::gopher; # <draft-murali-url-gopher>, Dec 4, 1996
  2. require URI::_server;
  3. @ISA=qw(URI::_server);
  4. use strict;
  5. use URI::Escape qw(uri_unescape);
  6. # A Gopher URL follows the common internet scheme syntax as defined in
  7. # section 4.3 of [RFC-URL-SYNTAX]:
  8. #
  9. # gopher://<host>[:<port>]/<gopher-path>
  10. #
  11. # where
  12. #
  13. # <gopher-path> := <gopher-type><selector> |
  14. # <gopher-type><selector>%09<search> |
  15. # <gopher-type><selector>%09<search>%09<gopher+_string>
  16. #
  17. # <gopher-type> := '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7'
  18. # '8' | '9' | '+' | 'I' | 'g' | 'T'
  19. #
  20. # <selector> := *pchar Refer to RFC 1808 [4]
  21. # <search> := *pchar
  22. # <gopher+_string> := *uchar Refer to RFC 1738 [3]
  23. #
  24. # If the optional port is omitted, the port defaults to 70.
  25. sub default_port { 70 }
  26. sub _gopher_type
  27. {
  28. my $self = shift;
  29. my $path = $self->path_query;
  30. $path =~ s,^/,,;
  31. my $gtype = $1 if $path =~ s/^(.)//s;
  32. if (@_) {
  33. my $new_type = shift;
  34. if (defined($new_type)) {
  35. Carp::croak("Bad gopher type '$new_type'")
  36. unless $new_type =~ /^[0-9+IgT]$/;
  37. substr($path, 0, 0) = $new_type;
  38. $self->path_query($path);
  39. } else {
  40. Carp::croak("Can't delete gopher type when selector is present")
  41. if length($path);
  42. $self->path_query(undef);
  43. }
  44. }
  45. return $gtype;
  46. }
  47. sub gopher_type
  48. {
  49. my $self = shift;
  50. my $gtype = $self->_gopher_type(@_);
  51. $gtype = "1" unless defined $gtype;
  52. $gtype;
  53. }
  54. *gtype = \&gopher_type; # URI::URL compatibility
  55. sub selector { shift->_gfield(0, @_) }
  56. sub search { shift->_gfield(1, @_) }
  57. sub string { shift->_gfield(2, @_) }
  58. sub _gfield
  59. {
  60. my $self = shift;
  61. my $fno = shift;
  62. my $path = $self->path_query;
  63. # not according to spec., but many popular browsers accept
  64. # gopher URLs with a '?' before the search string.
  65. $path =~ s/\?/\t/;
  66. $path = uri_unescape($path);
  67. $path =~ s,^/,,;
  68. my $gtype = $1 if $path =~ s,^(.),,s;
  69. my @path = split(/\t/, $path, 3);
  70. if (@_) {
  71. # modify
  72. my $new = shift;
  73. $path[$fno] = $new;
  74. pop(@path) while @path && !defined($path[-1]);
  75. for (@path) { $_="" unless defined }
  76. $path = $gtype;
  77. $path = "1" unless defined $path;
  78. $path .= join("\t", @path);
  79. $self->path_query($path);
  80. }
  81. $path[$fno];
  82. }
  83. 1;