@@ -8,6 +8,7 @@ use Module::InstalledVersion;
8
8
use Moose;
9
9
use Parse::CPAN::Authors;
10
10
use Parse::CPAN::Packages;
11
+ use Parse::CPAN::Whois;
11
12
use Parse::CPAN::Meta;
12
13
use Pod::Simple::HTML;
13
14
use Path::Class;
@@ -27,7 +28,8 @@ has 'hostname' => ( is => 'rw' );
27
28
has ' cgi' => ( is => ' rw' , isa => ' CGI' );
28
29
has ' directory' => ( is => ' rw' , isa => ' Path::Class::Dir' );
29
30
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' );
31
33
has ' parse_cpan_packages' => ( is => ' rw' , isa => ' Parse::CPAN::Packages' );
32
34
has ' pauseid' => ( is => ' rw' );
33
35
has ' distvname' => ( is => ' rw' );
@@ -91,6 +93,22 @@ sub checksum_data_for_author {
91
93
return $cksum ;
92
94
}
93
95
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
+
94
112
# this is a hook that HTTP::Server::Simple calls after setting up the
95
113
# listening socket. we use it load the indexes
96
114
sub after_setup_listener {
@@ -107,8 +125,18 @@ sub after_setup_listener {
107
125
&& ( -f $authors_filename )
108
126
&& ( -f $packages_filename );
109
127
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
+ }
112
140
my $parse_cpan_packages = $cache -> get_code( ' parse_cpan_packages' ,
113
141
sub { Parse::CPAN::Packages-> new( $packages_filename -> stringify ) } );
114
142
@@ -136,8 +164,8 @@ sub handle_request {
136
164
my ( $self , $cgi ) = @_ ;
137
165
eval { $self -> _handle_request($cgi ) };
138
166
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($@ );
141
169
}
142
170
}
143
171
@@ -215,8 +243,7 @@ sub not_found_page {
215
243
my $self = shift ;
216
244
my $q = shift ;
217
245
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' );
220
247
print Template::Declare-> show(
221
248
' 404' ,
222
249
{ parse_cpan_authors => $self -> parse_cpan_authors,
@@ -231,28 +258,25 @@ sub not_found_page {
231
258
sub redirect {
232
259
my $self = shift ;
233
260
my $url = shift ;
234
- print " HTTP/1.0 302 OK\r\n " ;
261
+
262
+ print " HTTP/1.0 302\015\012 " ;
235
263
print $self -> cgi-> redirect($url );
236
264
237
265
}
238
266
239
267
sub index_page {
240
268
my $self = shift ;
241
- my $cgi = $self -> cgi;
242
269
243
- print " HTTP/1.0 200 OK\r\n " ;
244
- print $cgi -> header;
270
+ $self -> send_http_header(200, -charset => ' utf-8' );
245
271
print Template::Declare-> show(' index' );
246
272
}
247
273
248
274
sub search_page {
249
275
my $self = shift ;
250
- my $cgi = $self -> cgi;
251
- my $q = $cgi -> param(' q' );
276
+ my $q = $self -> cgi-> param(' q' );
252
277
253
278
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' );
256
280
print Template::Declare-> show(
257
281
' search' ,
258
282
{ parse_cpan_authors => $self -> parse_cpan_authors,
@@ -269,10 +293,11 @@ sub _do_search {
269
293
my $q = shift ;
270
294
my $index = $self -> index ;
271
295
my @results = $index -> search($q );
296
+ my $au_type = $self -> author_type;
272
297
my ( @authors , @distributions , @packages );
273
298
274
299
if ( $q !~ / (?:::|-)/ ) {
275
- @authors = uniq grep { ref ($_ ) eq ' Parse::CPAN::Authors ::Author' }
300
+ @authors = uniq grep { ref ($_ ) eq " Parse::CPAN::${au_type} ::Author" }
276
301
@results ;
277
302
}
278
303
if ( $q !~ / ::/ ) {
@@ -307,7 +332,6 @@ sub _do_search {
307
332
308
333
sub author_page {
309
334
my $self = shift ;
310
- my $cgi = $self -> cgi;
311
335
my $pauseid = $self -> pauseid;
312
336
313
337
my @distributions = sort { $a -> distvname cmp $b -> distvname }
@@ -323,8 +347,7 @@ sub author_page {
323
347
}
324
348
}
325
349
326
- print " HTTP/1.0 200 OK\r\n " ;
327
- print $cgi -> header;
350
+ $self -> send_http_header(200, -charset => ' utf-8' );
328
351
print Template::Declare-> show(
329
352
' author' ,
330
353
{ author => $author ,
@@ -337,7 +360,6 @@ sub author_page {
337
360
338
361
sub distribution_page {
339
362
my $self = shift ;
340
- my $cgi = $self -> cgi;
341
363
my $pauseid = $self -> pauseid;
342
364
my $distvname = $self -> distvname;
343
365
@@ -359,8 +381,7 @@ sub distribution_page {
359
381
360
382
my @filenames = $self -> list_files($distribution );
361
383
362
- print " HTTP/1.0 200 OK\r\n " ;
363
- print $cgi -> header;
384
+ $self -> send_http_header(200, -charset => ' utf-8' );
364
385
print Template::Declare-> show(
365
386
' distribution' ,
366
387
{ author => $self -> parse_cpan_authors-> author( uc $pauseid ),
@@ -376,8 +397,7 @@ sub distribution_page {
376
397
377
398
sub pod_page {
378
399
my $self = shift ;
379
- my $cgi = $self -> cgi;
380
- my ($pkgname ) = $cgi -> keywords;
400
+ my ($pkgname ) = $self -> cgi-> keywords;
381
401
382
402
my $m = $self -> parse_cpan_packages-> package($pkgname );
383
403
my $d = $m -> distribution;
@@ -390,7 +410,6 @@ sub pod_page {
390
410
391
411
sub install_page {
392
412
my $self = shift ;
393
- my $cgi = $self -> cgi;
394
413
my $pauseid = $self -> pauseid;
395
414
my $distvname = $self -> distvname;
396
415
@@ -401,8 +420,7 @@ sub install_page {
401
420
my $file
402
421
= file( $self -> directory, ' authors' , ' id' , $distribution -> prefix );
403
422
404
- print " HTTP/1.0 200 OK\r\n " ;
405
- print $cgi -> header;
423
+ $self -> send_http_header(200);
406
424
printf ' <html><body><h1>Installing %s</h1><pre>' ,
407
425
$distribution -> distvname;
408
426
@@ -417,7 +435,6 @@ sub install_page {
417
435
418
436
sub file_page {
419
437
my $self = shift ;
420
- my $cgi = $self -> cgi;
421
438
my $pauseid = $self -> pauseid;
422
439
my $distvname = $self -> distvname;
423
440
my $filename = $self -> filename;
@@ -443,8 +460,7 @@ sub file_page {
443
460
# $html
444
461
# =~ s/^(.*%3A%3A.*)$/my $x = $1; ($x =~ m{indexItem}) ? 1 : $x =~ s{%3A%3A}{\/}g; $x/gme;
445
462
446
- print " HTTP/1.0 200 OK\r\n " ;
447
- print $cgi -> header;
463
+ $self -> send_http_header(200, -charset => ' utf-8' );
448
464
print Template::Declare-> show(
449
465
' file' ,
450
466
{ author => $self -> parse_cpan_authors-> author( uc $pauseid ),
@@ -460,7 +476,6 @@ sub file_page {
460
476
461
477
sub download_file {
462
478
my $self = shift ;
463
- my $cgi = $self -> cgi;
464
479
my $pauseid = $self -> pauseid;
465
480
my $distvname = $self -> distvname;
466
481
my $filename = $self -> filename;
@@ -475,19 +490,17 @@ sub download_file {
475
490
if ($filename ) {
476
491
my $contents
477
492
= $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' ,
481
495
-content_length => length $contents ,
482
496
);
483
497
print $contents ;
484
498
} else {
485
499
open my $fh , $file or return $self -> not_found_page( $self -> filename );
486
500
487
- print " HTTP/1.0 200 OK\r\n " ;
488
501
my $content_type
489
502
= $file =~ / zip/ ? ' application/zip' : ' application/x-gzip' ;
490
- print $cgi -> header(
503
+ $self -> send_http_header(200,
491
504
-content_type => $content_type ,
492
505
-content_disposition => " attachment; filename=" . $file -> basename,
493
506
-content_length => -s $fh ,
@@ -500,7 +513,6 @@ sub download_file {
500
513
501
514
sub raw_page {
502
515
my $self = shift ;
503
- my $cgi = $self -> cgi;
504
516
my $pauseid = $self -> pauseid;
505
517
my $distvname = $self -> distvname;
506
518
my $filename = $self -> filename;
@@ -546,8 +558,7 @@ sub raw_page {
546
558
$html = join ' ' , @lines ;
547
559
}
548
560
549
- print " HTTP/1.0 200 OK\r\n " ;
550
- print $cgi -> header;
561
+ $self -> send_http_header(200, -charset => ' utf-8' );
551
562
print Template::Declare-> show(
552
563
' raw' ,
553
564
{ author => $self -> parse_cpan_authors-> author( uc $pauseid ),
@@ -574,8 +585,7 @@ sub dist_page {
574
585
575
586
sub package_page {
576
587
my $self = shift ;
577
- my $cgi = $self -> cgi;
578
- my $path = $cgi -> path_info();
588
+ my $path = $self -> cgi-> path_info();
579
589
my ( $pauseid , $distvname , $package )
580
590
= $path =~ m { ^/package/(.+?)/(.+?)/(.+?)/$} ;
581
591
@@ -622,55 +632,45 @@ sub list_files {
622
632
623
633
sub css_screen_page {
624
634
my $self = shift ;
625
- my $cgi = $self -> cgi;
626
635
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' );
629
637
print Template::Declare-> show(' css_screen' );
630
638
}
631
639
632
640
sub css_print_page {
633
641
my $self = shift ;
634
- my $cgi = $self -> cgi;
635
642
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' );
638
644
print Template::Declare-> show(' css_print' );
639
645
}
640
646
641
647
sub css_ie_page {
642
648
my $self = shift ;
643
649
my $cgi = $self -> cgi;
644
650
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' );
647
652
print Template::Declare-> show(' css_ie' );
648
653
}
649
654
650
655
sub images_logo_page {
651
656
my $self = shift ;
652
- my $cgi = $self -> cgi;
653
657
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' );
656
659
print Template::Declare-> show(' images_logo' );
657
660
}
658
661
659
662
sub images_favicon_page {
660
663
my $self = shift ;
661
- my $cgi = $self -> cgi;
662
664
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' );
665
666
print Template::Declare-> show(' images_favicon' );
666
667
}
667
668
668
669
sub opensearch_page {
669
670
my $self = shift ;
670
671
my $cgi = $self -> cgi;
671
672
672
- print " HTTP/1.0 200 OK\r\n " ;
673
- print $cgi -> header(
673
+ $self -> send_http_header( 200,
674
674
-type => ' application/opensearchdescription+xml' ,
675
675
-expires => ' +1d'
676
676
);
0 commit comments