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.

142 lines
3.8 KiB

  1. #
  2. # $Id: nntp.pm,v 1.8 1998/11/19 21:45:02 aas Exp $
  3. # Implementation of the Network News Transfer Protocol (RFC 977)
  4. #
  5. package LWP::Protocol::nntp;
  6. require LWP::Protocol;
  7. @ISA = qw(LWP::Protocol);
  8. require LWP::Debug;
  9. require HTTP::Response;
  10. require HTTP::Status;
  11. require Net::NNTP;
  12. use strict;
  13. sub request
  14. {
  15. my($self, $request, $proxy, $arg, $size, $timeout) = @_;
  16. LWP::Debug::trace('()');
  17. $size = 4096 unless $size;
  18. # Check for proxy
  19. if (defined $proxy) {
  20. return HTTP::Response->new(&HTTP::Status::RC_BAD_REQUEST,
  21. 'You can not proxy through NNTP');
  22. }
  23. # Check that the scheme is as expected
  24. my $url = $request->url;
  25. my $scheme = $url->scheme;
  26. unless ($scheme eq 'news') {
  27. return HTTP::Response->new(&HTTP::Status::RC_INTERNAL_SERVER_ERROR,
  28. "LWP::Protocol::nntp::request called for '$scheme'");
  29. }
  30. # check for a valid method
  31. my $method = $request->method;
  32. unless ($method eq 'GET' || $method eq 'HEAD' || $method eq 'POST') {
  33. return HTTP::Response->new(&HTTP::Status::RC_BAD_REQUEST,
  34. 'Library does not allow method ' .
  35. "$method for 'news:' URLs");
  36. }
  37. # extract the identifier and check against posting to an article
  38. my $groupart = $url->_group;
  39. my $is_art = $groupart =~ /@/;
  40. if ($is_art && $method eq 'POST') {
  41. return HTTP::Response->new(&HTTP::Status::RC_BAD_REQUEST,
  42. "Can't post to an article <$groupart>");
  43. }
  44. my $nntp = Net::NNTP->new(undef,
  45. #Port => 18574,
  46. Timeout => $timeout,
  47. #Debug => 1,
  48. );
  49. die "Can't connect to nntp server" unless $nntp;
  50. # Check the initial welcome message from the NNTP server
  51. if ($nntp->status != 2) {
  52. return HTTP::Response->new(&HTTP::Status::RC_SERVICE_UNAVAILABLE,
  53. $nntp->message);
  54. }
  55. my $response = HTTP::Response->new(&HTTP::Status::RC_OK, "OK");
  56. my $mess = $nntp->message;
  57. LWP::Debug::debug($mess);
  58. # Try to extract server name from greating message.
  59. # Don't know if this works well for a large class of servers, but
  60. # this works for our server.
  61. $mess =~ s/\s+ready\b.*//;
  62. $mess =~ s/^\S+\s+//;
  63. $response->header(Server => $mess);
  64. # First we handle posting of articles
  65. if ($method eq 'POST') {
  66. return HTTP::Response->new(&HTTP::Status::RC_NOT_IMPLEMENTED,
  67. "POST not implemented yet");
  68. }
  69. # The method must be "GET" or "HEAD" by now
  70. if (!$is_art) {
  71. if (!$nntp->group($groupart)) {
  72. return HTTP::Response->new(&HTTP::Status::RC_NOT_FOUND,
  73. $nntp->message);
  74. }
  75. return HTTP::Response->new(&HTTP::Status::RC_NOT_IMPLEMENTED,
  76. "GET newsgroup not implemented yet");
  77. }
  78. # Send command to server to retrieve an article (or just the headers)
  79. my $get = $method eq 'HEAD' ? "head" : "article";
  80. my $art = $nntp->$get("<$groupart>");
  81. unless ($art) {
  82. return HTTP::Response->new(&HTTP::Status::RC_NOT_FOUND,
  83. $nntp->message);
  84. }
  85. LWP::Debug::debug($nntp->message);
  86. # Parse headers
  87. my($key, $val);
  88. while ($_ = shift @$art) {
  89. if (/^\s+$/) {
  90. last; # end of headers
  91. } elsif (/^(\S+):\s*(.*)/) {
  92. $response->push_header($key, $val) if $key;
  93. ($key, $val) = ($1, $2);
  94. } elsif (/^\s+(.*)/) {
  95. next unless $key;
  96. $val .= $1;
  97. } else {
  98. unshift(@$art, $_);
  99. last;
  100. }
  101. }
  102. $response->push_header($key, $val) if $key;
  103. # Ensure that there is a Content-Type header
  104. $response->header("Content-Type", "text/plain")
  105. unless $response->header("Content-Type");
  106. # Collect the body
  107. $response = $self->collect_once($arg, $response, join("", @$art))
  108. if @$art;
  109. # Say godbye to the server
  110. $nntp->quit;
  111. $nntp = undef;
  112. $response;
  113. }
  114. 1;