1 package URI::gopher; # <draft-murali-url-gopher>, Dec 4, 1996
7 use URI::Escape qw(uri_unescape);
9 # A Gopher URL follows the common internet scheme syntax as defined in
10 # section 4.3 of [RFC-URL-SYNTAX]:
12 # gopher://<host>[:<port>]/<gopher-path>
16 # <gopher-path> := <gopher-type><selector> |
17 # <gopher-type><selector>%09<search> |
18 # <gopher-type><selector>%09<search>%09<gopher+_string>
20 # <gopher-type> := '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7'
21 # '8' | '9' | '+' | 'I' | 'g' | 'T'
23 # <selector> := *pchar Refer to RFC 1808 [4]
25 # <gopher+_string> := *uchar Refer to RFC 1738 [3]
27 # If the optional port is omitted, the port defaults to 70.
29 sub default_port { 70 }
34 my $path = $self->path_query;
36 my $gtype = $1 if $path =~ s/^(.)//s;
39 if (defined($new_type)) {
40 Carp::croak("Bad gopher type '$new_type'")
41 unless length($new_type) == 1;
42 substr($path, 0, 0) = $new_type;
43 $self->path_query($path);
45 Carp::croak("Can't delete gopher type when selector is present")
47 $self->path_query(undef);
56 my $gtype = $self->_gopher_type(@_);
57 $gtype = "1" unless defined $gtype;
61 *gtype = \&gopher_type; # URI::URL compatibility
63 sub selector { shift->_gfield(0, @_) }
64 sub search { shift->_gfield(1, @_) }
65 sub string { shift->_gfield(2, @_) }
71 my $path = $self->path_query;
73 # not according to spec., but many popular browsers accept
74 # gopher URLs with a '?' before the search string.
76 $path = uri_unescape($path);
78 my $gtype = $1 if $path =~ s,^(.),,s;
79 my @path = split(/\t/, $path, 3);
84 pop(@path) while @path && !defined($path[-1]);
85 for (@path) { $_="" unless defined }
87 $path = "1" unless defined $path;
88 $path .= join("\t", @path);
89 $self->path_query($path);