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.

216 lines
5.9 KiB

  1. #
  2. # $Id: gopher.pm,v 1.19 1998/11/19 20:28:40 aas Exp $
  3. # Implementation of the gopher protocol (RFC 1436)
  4. #
  5. # This code is based on 'wwwgopher.pl,v 0.10 1994/10/17 18:12:34 shelden'
  6. # which in turn is a vastly modified version of Oscar's http'get()
  7. # dated 28/3/94 in <ftp://cui.unige.ch/PUBLIC/oscar/scripts/http.pl>
  8. # including contributions from Marc van Heyningen and Martijn Koster.
  9. #
  10. package LWP::Protocol::gopher;
  11. use strict;
  12. use vars qw(@ISA);
  13. require HTTP::Response;
  14. require HTTP::Status;
  15. require IO::Socket;
  16. require IO::Select;
  17. require LWP::Protocol;
  18. @ISA = qw(LWP::Protocol);
  19. my %gopher2mimetype = (
  20. '0' => 'text/plain', # 0 file
  21. '1' => 'text/html', # 1 menu
  22. # 2 CSO phone-book server
  23. # 3 Error
  24. '4' => 'application/mac-binhex40', # 4 BinHexed Macintosh file
  25. '5' => 'application/zip', # 5 DOS binary archive of some sort
  26. '6' => 'application/octet-stream', # 6 UNIX uuencoded file.
  27. '7' => 'text/html', # 7 Index-Search server
  28. # 8 telnet session
  29. '9' => 'application/octet-stream', # 9 binary file
  30. 'h' => 'text/html', # html
  31. 'g' => 'image/gif', # gif
  32. 'I' => 'image/*', # some kind of image
  33. );
  34. my %gopher2encoding = (
  35. '6' => 'x_uuencode', # 6 UNIX uuencoded file.
  36. );
  37. sub request
  38. {
  39. my($self, $request, $proxy, $arg, $size, $timeout) = @_;
  40. LWP::Debug::trace('()');
  41. $size = 4096 unless $size;
  42. # check proxy
  43. if (defined $proxy) {
  44. return HTTP::Response->new(&HTTP::Status::RC_BAD_REQUEST,
  45. 'You can not proxy through the gopher');
  46. }
  47. my $url = $request->url;
  48. die "bad scheme" if $url->scheme ne 'gopher';
  49. my $method = $request->method;
  50. unless ($method eq 'GET' || $method eq 'HEAD') {
  51. return HTTP::Response->new(&HTTP::Status::RC_BAD_REQUEST,
  52. 'Library does not allow method ' .
  53. "$method for 'gopher:' URLs");
  54. }
  55. my $gophertype = $url->gopher_type;
  56. unless (exists $gopher2mimetype{$gophertype}) {
  57. return HTTP::Response->new(&HTTP::Status::RC_NOT_IMPLEMENTED,
  58. 'Library does not support gophertype ' .
  59. $gophertype);
  60. }
  61. my $response = HTTP::Response->new(&HTTP::Status::RC_OK, "OK");
  62. $response->header('Content-type' => $gopher2mimetype{$gophertype}
  63. || 'text/plain');
  64. $response->header('Content-Encoding' => $gopher2encoding{$gophertype})
  65. if exists $gopher2encoding{$gophertype};
  66. if ($method eq 'HEAD') {
  67. # XXX: don't even try it so we set this header
  68. $response->header('Client-Warning' => 'Client answer only');
  69. return $response;
  70. }
  71. if ($gophertype eq '7' && ! $url->search) {
  72. # the url is the prompt for a gopher search; supply boiler-plate
  73. return $self->collect_once($arg, $response, <<"EOT");
  74. <HEAD>
  75. <TITLE>Gopher Index</TITLE>
  76. <ISINDEX>
  77. </HEAD>
  78. <BODY>
  79. <H1>$url<BR>Gopher Search</H1>
  80. This is a searchable Gopher index.
  81. Use the search function of your browser to enter search terms.
  82. </BODY>
  83. EOT
  84. }
  85. my $host = $url->host;
  86. my $port = $url->port;
  87. my $requestLine = "";
  88. my $selector = $url->selector;
  89. if (defined $selector) {
  90. $requestLine .= $selector;
  91. my $search = $url->search;
  92. if (defined $search) {
  93. $requestLine .= "\t$search";
  94. my $string = $url->string;
  95. if (defined $string) {
  96. $requestLine .= "\t$string";
  97. }
  98. }
  99. }
  100. $requestLine .= "\015\012";
  101. # potential request headers are just ignored
  102. # Ok, lets make the request
  103. my $socket = IO::Socket::INET->new(PeerAddr => $host,
  104. PeerPort => $port,
  105. Proto => 'tcp',
  106. Timeout => $timeout);
  107. die "Can't connect to $host:$port" unless $socket;
  108. my $sel = IO::Select->new($socket);
  109. {
  110. die "write timeout" if $timeout && !$sel->can_write($timeout);
  111. my $n = syswrite($socket, $requestLine, length($requestLine));
  112. die $! unless defined($n);
  113. die "short write" if $n != length($requestLine);
  114. }
  115. my $user_arg = $arg;
  116. # must handle menus in a special way since they are to be
  117. # converted to HTML. Undefing $arg ensures that the user does
  118. # not see the data before we get a change to convert it.
  119. $arg = undef if $gophertype eq '1' || $gophertype eq '7';
  120. # collect response
  121. my $buf = '';
  122. $response = $self->collect($arg, $response, sub {
  123. die "read timeout" if $timeout && !$sel->can_read($timeout);
  124. my $n = sysread($socket, $buf, $size);
  125. die $! unless defined($n);
  126. return \$buf;
  127. } );
  128. # Convert menu to HTML and return data to user.
  129. if ($gophertype eq '1' || $gophertype eq '7') {
  130. my $content = menu2html($response->content);
  131. if (defined $user_arg) {
  132. $response = $self->collect_once($user_arg, $response, $content);
  133. } else {
  134. $response->content($content);
  135. }
  136. }
  137. $response;
  138. }
  139. sub gopher2url
  140. {
  141. my($gophertype, $path, $host, $port) = @_;
  142. my $url;
  143. if ($gophertype eq '8' || $gophertype eq 'T') {
  144. # telnet session
  145. $url = $HTTP::URI_CLASS->new($gophertype eq '8' ? 'telnet:':'tn3270:');
  146. $url->user($path) if defined $path;
  147. } else {
  148. $path = URI::Escape::uri_escape($path);
  149. $url = $HTTP::URI_CLASS->new("gopher:/$gophertype$path");
  150. }
  151. $url->host($host);
  152. $url->port($port);
  153. $url;
  154. }
  155. sub menu2html {
  156. my($menu) = @_;
  157. $menu =~ s/\015//g; # remove carriage return
  158. my $tmp = <<"EOT";
  159. <HTML>
  160. <HEAD>
  161. <TITLE>Gopher menu</TITLE>
  162. </HEAD>
  163. <BODY>
  164. <H1>Gopher menu</H1>
  165. EOT
  166. for (split("\n", $menu)) {
  167. last if /^\./;
  168. my($pretty, $path, $host, $port) = split("\t");
  169. $pretty =~ s/^(.)//;
  170. my $type = $1;
  171. my $url = gopher2url($type, $path, $host, $port)->as_string;
  172. $tmp .= qq{<A HREF="$url">$pretty</A><BR>\n};
  173. }
  174. $tmp .= "</BODY>\n</HTML>\n";
  175. $tmp;
  176. }
  177. 1;