Skip to content

Commit 492e46d

Browse files
author
Martijn van Beers
committed
Merge branch 'whois'
1 parent d5532e9 commit 492e46d

File tree

4 files changed

+70
-63
lines changed

4 files changed

+70
-63
lines changed

Diff for: Makefile.PL

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ WriteMakefile(
1818
'Module::InstalledVersion' => '0',
1919
'Moose' => '0',
2020
'Parse::CPAN::Authors' => '0',
21+
'Parse::CPAN::Whois ' => '0.02',
2122
'Parse::CPAN::Meta' => '0',
2223
'Parse::CPAN::Packages' => '2.28',
2324
'Path::Class' => '0',

Diff for: lib/CPAN/Mini/Webserver.pm

+57-57
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use Module::InstalledVersion;
88
use Moose;
99
use Parse::CPAN::Authors;
1010
use Parse::CPAN::Packages;
11+
use Parse::CPAN::Whois;
1112
use Parse::CPAN::Meta;
1213
use Pod::Simple::HTML;
1314
use Path::Class;
@@ -27,7 +28,8 @@ has 'hostname' => ( is => 'rw' );
2728
has 'cgi' => ( is => 'rw', isa => 'CGI' );
2829
has 'directory' => ( is => 'rw', isa => 'Path::Class::Dir' );
2930
has 'scratch' => ( is => 'rw', isa => 'Path::Class::Dir' );
30-
has 'parse_cpan_authors' => ( is => 'rw', isa => 'Parse::CPAN::Authors' );
31+
has 'author_type' => ( is => 'rw' );
32+
has 'parse_cpan_authors' => ( is => 'rw' );
3133
has 'parse_cpan_packages' => ( is => 'rw', isa => 'Parse::CPAN::Packages' );
3234
has 'pauseid' => ( is => 'rw' );
3335
has 'distvname' => ( is => 'rw' );
@@ -91,6 +93,22 @@ sub checksum_data_for_author {
9193
return $cksum;
9294
}
9395

96+
sub send_http_header {
97+
my $self = shift;
98+
my $code = shift;
99+
my %params = @_;
100+
my $cgi = $self->cgi;
101+
102+
if ( ( defined $params{-charset} and $params{-charset} eq 'utf-8' ) or
103+
( defined $params{-type} and $params{-type} eq 'text/xml' ) ) {
104+
binmode (STDOUT, ":encoding(utf-8)");
105+
} elsif (defined $params{-type}) {
106+
binmode STDOUT, ":raw";
107+
}
108+
print "HTTP/1.0 $code\015\012";
109+
print $cgi->header(%params);
110+
}
111+
94112
# this is a hook that HTTP::Server::Simple calls after setting up the
95113
# listening socket. we use it load the indexes
96114
sub after_setup_listener {
@@ -107,8 +125,18 @@ sub after_setup_listener {
107125
&& ( -f $authors_filename )
108126
&& ( -f $packages_filename );
109127
my $cache = App::Cache->new( { ttl => 60 * 60 } );
110-
my $parse_cpan_authors = $cache->get_code( 'parse_cpan_authors',
111-
sub { Parse::CPAN::Authors->new( $authors_filename->stringify ) } );
128+
129+
my $whois_filename = file( $directory, 'authors', '00whois.xml' );
130+
my $parse_cpan_authors;
131+
if ( -f $whois_filename) {
132+
$self->author_type('Whois');
133+
$parse_cpan_authors = $cache->get_code( 'parse_cpan_whois',
134+
sub { Parse::CPAN::Whois->new ( $whois_filename->stringify ) } );
135+
} else {
136+
$self->author_type('Authors');
137+
$parse_cpan_authors = $cache->get_code( 'parse_cpan_authors',
138+
sub { Parse::CPAN::Authors->new( $authors_filename->stringify ) } );
139+
}
112140
my $parse_cpan_packages = $cache->get_code( 'parse_cpan_packages',
113141
sub { Parse::CPAN::Packages->new( $packages_filename->stringify ) } );
114142

@@ -136,8 +164,8 @@ sub handle_request {
136164
my ( $self, $cgi ) = @_;
137165
eval { $self->_handle_request($cgi) };
138166
if ($@) {
139-
print "HTTP/1.0 500\r\n", $cgi->header,
140-
"<h1>Internal Server Error</h1>", $cgi->escapeHTML($@);
167+
$self->send_http_header(500);
168+
print "<h1>Internal Server Error</h1>", $cgi->escapeHTML($@);
141169
}
142170
}
143171

@@ -215,8 +243,7 @@ sub not_found_page {
215243
my $self = shift;
216244
my $q = shift;
217245
my ( $authors, $dists, $packages ) = $self->_do_search($q);
218-
print "HTTP/1.0 404 Not found\r\n";
219-
print $self->cgi->header;
246+
$self->send_http_header(404, -charset => 'utf-8');
220247
print Template::Declare->show(
221248
'404',
222249
{ parse_cpan_authors => $self->parse_cpan_authors,
@@ -231,28 +258,25 @@ sub not_found_page {
231258
sub redirect {
232259
my $self = shift;
233260
my $url = shift;
234-
print "HTTP/1.0 302 OK\r\n";
261+
262+
print "HTTP/1.0 302\015\012";
235263
print $self->cgi->redirect($url);
236264

237265
}
238266

239267
sub index_page {
240268
my $self = shift;
241-
my $cgi = $self->cgi;
242269

243-
print "HTTP/1.0 200 OK\r\n";
244-
print $cgi->header;
270+
$self->send_http_header(200, -charset => 'utf-8');
245271
print Template::Declare->show('index');
246272
}
247273

248274
sub search_page {
249275
my $self = shift;
250-
my $cgi = $self->cgi;
251-
my $q = $cgi->param('q');
276+
my $q = $self->cgi->param('q');
252277

253278
my ( $authors, $dists, $packages ) = $self->_do_search($q);
254-
print "HTTP/1.0 200 OK\r\n";
255-
print $cgi->header;
279+
$self->send_http_header(200, -charset => 'utf-8');
256280
print Template::Declare->show(
257281
'search',
258282
{ parse_cpan_authors => $self->parse_cpan_authors,
@@ -269,10 +293,11 @@ sub _do_search {
269293
my $q = shift;
270294
my $index = $self->index;
271295
my @results = $index->search($q);
296+
my $au_type = $self->author_type;
272297
my ( @authors, @distributions, @packages );
273298

274299
if ( $q !~ /(?:::|-)/ ) {
275-
@authors = uniq grep { ref($_) eq 'Parse::CPAN::Authors::Author' }
300+
@authors = uniq grep { ref($_) eq "Parse::CPAN::${au_type}::Author" }
276301
@results;
277302
}
278303
if ( $q !~ /::/ ) {
@@ -307,7 +332,6 @@ sub _do_search {
307332

308333
sub author_page {
309334
my $self = shift;
310-
my $cgi = $self->cgi;
311335
my $pauseid = $self->pauseid;
312336

313337
my @distributions = sort { $a->distvname cmp $b->distvname }
@@ -323,8 +347,7 @@ sub author_page {
323347
}
324348
}
325349

326-
print "HTTP/1.0 200 OK\r\n";
327-
print $cgi->header;
350+
$self->send_http_header(200, -charset => 'utf-8');
328351
print Template::Declare->show(
329352
'author',
330353
{ author => $author,
@@ -337,7 +360,6 @@ sub author_page {
337360

338361
sub distribution_page {
339362
my $self = shift;
340-
my $cgi = $self->cgi;
341363
my $pauseid = $self->pauseid;
342364
my $distvname = $self->distvname;
343365

@@ -359,8 +381,7 @@ sub distribution_page {
359381

360382
my @filenames = $self->list_files($distribution);
361383

362-
print "HTTP/1.0 200 OK\r\n";
363-
print $cgi->header;
384+
$self->send_http_header(200, -charset => 'utf-8');
364385
print Template::Declare->show(
365386
'distribution',
366387
{ author => $self->parse_cpan_authors->author( uc $pauseid ),
@@ -376,8 +397,7 @@ sub distribution_page {
376397

377398
sub pod_page {
378399
my $self = shift;
379-
my $cgi = $self->cgi;
380-
my ($pkgname) = $cgi->keywords;
400+
my ($pkgname) = $self->cgi->keywords;
381401

382402
my $m = $self->parse_cpan_packages->package($pkgname);
383403
my $d = $m->distribution;
@@ -390,7 +410,6 @@ sub pod_page {
390410

391411
sub install_page {
392412
my $self = shift;
393-
my $cgi = $self->cgi;
394413
my $pauseid = $self->pauseid;
395414
my $distvname = $self->distvname;
396415

@@ -401,8 +420,7 @@ sub install_page {
401420
my $file
402421
= file( $self->directory, 'authors', 'id', $distribution->prefix );
403422

404-
print "HTTP/1.0 200 OK\r\n";
405-
print $cgi->header;
423+
$self->send_http_header(200);
406424
printf '<html><body><h1>Installing %s</h1><pre>',
407425
$distribution->distvname;
408426

@@ -417,7 +435,6 @@ sub install_page {
417435

418436
sub file_page {
419437
my $self = shift;
420-
my $cgi = $self->cgi;
421438
my $pauseid = $self->pauseid;
422439
my $distvname = $self->distvname;
423440
my $filename = $self->filename;
@@ -443,8 +460,7 @@ sub file_page {
443460
# $html
444461
# =~ s/^(.*%3A%3A.*)$/my $x = $1; ($x =~ m{indexItem}) ? 1 : $x =~ s{%3A%3A}{\/}g; $x/gme;
445462

446-
print "HTTP/1.0 200 OK\r\n";
447-
print $cgi->header;
463+
$self->send_http_header(200, -charset => 'utf-8');
448464
print Template::Declare->show(
449465
'file',
450466
{ author => $self->parse_cpan_authors->author( uc $pauseid ),
@@ -460,7 +476,6 @@ sub file_page {
460476

461477
sub download_file {
462478
my $self = shift;
463-
my $cgi = $self->cgi;
464479
my $pauseid = $self->pauseid;
465480
my $distvname = $self->distvname;
466481
my $filename = $self->filename;
@@ -475,19 +490,17 @@ sub download_file {
475490
if ($filename) {
476491
my $contents
477492
= $self->get_file_from_tarball( $distribution, $filename );
478-
print "HTTP/1.0 200 OK\r\n";
479-
print $cgi->header(
480-
-content_type => 'text/plain',
493+
$self->send_http_header(200,
494+
-content_type => 'text/plain',
481495
-content_length => length $contents,
482496
);
483497
print $contents;
484498
} else {
485499
open my $fh, $file or return $self->not_found_page( $self->filename );
486500

487-
print "HTTP/1.0 200 OK\r\n";
488501
my $content_type
489502
= $file =~ /zip/ ? 'application/zip' : 'application/x-gzip';
490-
print $cgi->header(
503+
$self->send_http_header(200,
491504
-content_type => $content_type,
492505
-content_disposition => "attachment; filename=" . $file->basename,
493506
-content_length => -s $fh,
@@ -500,7 +513,6 @@ sub download_file {
500513

501514
sub raw_page {
502515
my $self = shift;
503-
my $cgi = $self->cgi;
504516
my $pauseid = $self->pauseid;
505517
my $distvname = $self->distvname;
506518
my $filename = $self->filename;
@@ -546,8 +558,7 @@ sub raw_page {
546558
$html = join '', @lines;
547559
}
548560

549-
print "HTTP/1.0 200 OK\r\n";
550-
print $cgi->header;
561+
$self->send_http_header(200, -charset => 'utf-8');
551562
print Template::Declare->show(
552563
'raw',
553564
{ author => $self->parse_cpan_authors->author( uc $pauseid ),
@@ -574,8 +585,7 @@ sub dist_page {
574585

575586
sub package_page {
576587
my $self = shift;
577-
my $cgi = $self->cgi;
578-
my $path = $cgi->path_info();
588+
my $path = $self->cgi->path_info();
579589
my ( $pauseid, $distvname, $package )
580590
= $path =~ m{^/package/(.+?)/(.+?)/(.+?)/$};
581591

@@ -622,55 +632,45 @@ sub list_files {
622632

623633
sub css_screen_page {
624634
my $self = shift;
625-
my $cgi = $self->cgi;
626635

627-
print "HTTP/1.0 200 OK\r\n";
628-
print $cgi->header( -type => 'text/css', -expires => '+1d' );
636+
$self->send_http_header( 200, -type => 'text/css', -expires => '+1d' );
629637
print Template::Declare->show('css_screen');
630638
}
631639

632640
sub css_print_page {
633641
my $self = shift;
634-
my $cgi = $self->cgi;
635642

636-
print "HTTP/1.0 200 OK\r\n";
637-
print $cgi->header( -type => 'text/css', -expires => '+1d' );
643+
$self->send_http_header( 200, -type => 'text/css', -expires => '+1d' );
638644
print Template::Declare->show('css_print');
639645
}
640646

641647
sub css_ie_page {
642648
my $self = shift;
643649
my $cgi = $self->cgi;
644650

645-
print "HTTP/1.0 200 OK\r\n";
646-
print $cgi->header( -type => 'text/css', -expires => '+1d' );
651+
$self->send_http_header( 200, -type => 'text/css', -expires => '+1d' );
647652
print Template::Declare->show('css_ie');
648653
}
649654

650655
sub images_logo_page {
651656
my $self = shift;
652-
my $cgi = $self->cgi;
653657

654-
print "HTTP/1.0 200 OK\r\n";
655-
print $cgi->header( -type => 'image/png', -expires => '+1d' );
658+
$self->send_http_header( 200, -type => 'image/png', -expires => '+1d' );
656659
print Template::Declare->show('images_logo');
657660
}
658661

659662
sub images_favicon_page {
660663
my $self = shift;
661-
my $cgi = $self->cgi;
662664

663-
print "HTTP/1.0 200 OK\r\n";
664-
print $cgi->header( -type => 'image/png', -expires => '+1d' );
665+
$self->send_http_header( 200, -type => 'image/png', -expires => '+1d' );
665666
print Template::Declare->show('images_favicon');
666667
}
667668

668669
sub opensearch_page {
669670
my $self = shift;
670671
my $cgi = $self->cgi;
671672

672-
print "HTTP/1.0 200 OK\r\n";
673-
print $cgi->header(
673+
$self->send_http_header( 200,
674674
-type => 'application/opensearchdescription+xml',
675675
-expires => '+1d'
676676
);

Diff for: t/WebserverTester.pm

+5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ use warnings;
77
use Test::Builder;
88

99
use IO::Capture::Stdout;
10+
sub IO::Capture::Tie_STDx::BINMODE {
11+
# this is so we can call binmode to do utf-8 output
12+
# in the real world and still use IO::Capture::Stdout here.
13+
}
14+
1015
use HTTP::Response;
1116
use CGI;
1217

0 commit comments

Comments
 (0)