File Coverage

blib/lib/Test/Reporter/HTTPGateway.pm
Criterion Covered Total %
statement 21 56 37.5
branch 0 22 0.0
condition 0 11 0.0
subroutine 7 15 46.6
pod 7 7 100.0
total 35 111 31.5


line stmt bran cond sub pod time code
1 1     1   20682 use strict;
  1         2  
  1         33  
2 1     1   3 use warnings;
  1         2  
  1         23  
3 1     1   48 use 5.006;
  1         8  
  1         51  
4              
5             package Test::Reporter::HTTPGateway;
6              
7             =head1 NAME
8              
9             Test::Reporter::HTTPGateway - relay CPAN Testers reports received via HTTP
10              
11             =head1 DESCRIPTION
12              
13             The CPAN Testers report submission system is difficult for some clients to use,
14             because it is not available via HTTP, which is often one of the only protocols
15             allowed through firewalls.
16              
17             Test::Reporter::HTTPGateway is a very simple HTTP request handler that relays
18             HTTP requests to the CPAN Testers.
19              
20             =cut
21              
22 1     1   11135 use CGI ();
  1         22656  
  1         26  
23 1     1   931 use Email::Send ();
  1         34775  
  1         55  
24 1     1   13 use Email::Simple;
  1         4  
  1         36  
25 1     1   7 use Email::Simple::Creator;
  1         4  
  1         1125  
26              
27             our $VERSION = '0.001';
28              
29             =head1 METHODS
30              
31             =head2 mailer
32              
33             =head2 default_mailer
34              
35             The C method returns the Email::Send mailer to use. If no
36             C environment variable is set, it falls back
37             to the result of the C method, which defaults to SMTP.
38              
39             =cut
40              
41 0     0 1   sub default_mailer { 'SMTP' }
42 0 0   0 1   sub mailer { $ENV{TEST_REPORTER_HTTPGATEWAY_MAILER} || $_[0]->default_mailer }
43              
44             =head2 destination
45              
46             =head2 default_destination
47              
48             These act like the mailer methods, above. The default destination is the
49             cpan-testers address.
50              
51             =cut
52              
53 0     0 1   sub default_destination { 'cpan-testers@perl.org' }
54              
55             sub destination {
56 0 0   0 1   $ENV{TEST_REPORTER_HTTPGATEWAY_ADDRESS} || $_[0]->default_destination;
57             }
58              
59             =head2 key_allowed
60              
61             if ($gateway->key_allowed($key)) { ... }
62              
63             This method returns true if the user key given in the HTTP request is
64             acceptable for posting a report.
65              
66             Users wishing to operate a secure gateway should override this method.
67              
68             =cut
69              
70 0     0 1   sub key_allowed { 1 };
71              
72             =head2 handle
73              
74             This method handles a request to post a report. It may be handed a CGI
75             request, and will instantiate a new one if none is given.
76              
77             The request is expected to be a POST request with the following form values:
78              
79             from - the email address of the user filing the report
80             key - the user key of the user filing the report
81             subject - the subject of the report (generated by Test::Reporter)
82             via - the generator of the test report
83             report - the content of the report itself (the "comments")
84              
85             In general, these reports will be filed by Test::Reporter or CPAN::Reporter.
86              
87             =cut
88              
89             sub handle {
90 0     0 1   my ($self, $q) = @_;
91 0   0       $q ||= CGI->new;
92              
93 0           my %post = (
94             from => scalar $q->param('from'),
95             subject => scalar $q->param('subject'),
96             via => scalar $q->param('via'),
97             report => scalar $q->param('report'),
98             key => scalar $q->param('key'),
99             );
100              
101 0           eval {
102             # This was causing "cgi died ?" under lighttpd. Eh. -- rjbs, 2008-04-05
103             # die [ 405 => undef ] unless $q->request_method eq 'POST';
104              
105 0           for (qw(from subject via report)) {
106 0 0 0       die [ 500 => "missing $_ field" ]
107             unless defined $post{$_} and length $post{$_};
108              
109 0 0         next if $_ eq 'report';
110 0 0         die [ 500 => "invalid $_ field" ] if $post{$_} =~ /[\r\n]/;
111             }
112              
113 0 0         die [ 403 => "unknown user key" ] unless $self->key_allowed($post{key});
114              
115 0           my $via = $self->via;
116              
117 0           my $email = Email::Simple->create(
118             body => $post{report},
119             header => [
120             To => $self->destination,
121             From => $post{from},
122             Subject => $post{subject},
123             'X-Reported-Via' => "$via $VERSION relayed from $post{via}",
124             ],
125             );
126              
127 0           my $rv = Email::Send->new({ mailer => $self->mailer })->send($email);
128 0 0         die "$rv" unless $rv; # I hate you, Return::Value -- rjbs, 2008-04-05
129             };
130              
131 0 0         if (my $error = $@) {
132 0           my ($status, $msg);
133              
134 0 0         if (ref $error eq 'ARRAY') {
135 0           ($status, $msg) = @$error;
136             } else {
137 0           warn $error;
138             }
139              
140 0 0 0       $status = 500 unless $status and $status =~ /\A\d{3}\z/;
141 0   0       $msg ||= 'internal error';
142              
143 0           $self->_respond($status, "Report not sent: $msg");
144 0           return;
145             } else {
146 0           $self->_respond(200, 'Report sent.');
147 0           return;
148             }
149             }
150              
151             sub _respond {
152 0     0     my ($self, $code, $msg) = @_;
153              
154 0           print "Status: $code\n";
155 0           print "Content-type: text/plain\n\n";
156 0           print "$msg\n";
157             }
158              
159             =head2 via
160              
161             This method returns the name to be used when identifying relay. By default it
162             returns the relay's class.
163              
164             =cut
165              
166             sub via {
167 0     0 1   my ($self) = @_;
168 0 0         return ref $self ? ref $self : $self;
169             }
170              
171             =head1 COPYRIGHT AND AUTHOR
172              
173             This distribution was written by Ricardo Signes, Erjbs@cpan.orgE.
174              
175             Copyright 2008. This is free software, released under the same terms as perl
176             itself.
177              
178             =cut
179              
180             1;