17 |
use File::Slurp; |
use File::Slurp; |
18 |
#use JSON; |
#use JSON; |
19 |
use IO::Socket::INET; |
use IO::Socket::INET; |
|
use Module::Refresh; |
|
20 |
|
|
21 |
our $pids; |
our $pids; |
22 |
$pids = { httpd => $$ } unless defined $pids; # keep pids on refresh |
$pids = { httpd => $$ } unless defined $pids; # keep pids on refresh |
37 |
|
|
38 |
use html; |
use html; |
39 |
our $static_pids; |
our $static_pids; |
40 |
|
use progress_bar; |
|
use Time::HiRes qw/time/; |
|
41 |
|
|
42 |
sub static { |
sub static { |
43 |
my ($client,$path) = @_; |
my ($client,$path) = @_; |
46 |
|
|
47 |
return if ! -f $full; |
return if ! -f $full; |
48 |
|
|
|
my $start_t = time(); |
|
|
|
|
49 |
if ( my $pid = fork ) { |
if ( my $pid = fork ) { |
50 |
# parent |
# parent |
51 |
close($client); |
close($client); |
61 |
|
|
62 |
my $size = -s $full || return; |
my $size = -s $full || return; |
63 |
|
|
|
$client->autoflush(1); |
|
|
|
|
64 |
print $client "HTTP/1.0 200 OK\r\nContent-Type: $type\r\nContent-Length: $size\r\nConnection: close\r\n\r\n"; |
print $client "HTTP/1.0 200 OK\r\nContent-Type: $type\r\nContent-Length: $size\r\nConnection: close\r\n\r\n"; |
65 |
|
|
66 |
open(my $fh, $full); |
open(my $fh, $full); |
69 |
my $buff; |
my $buff; |
70 |
my $pos = 0; |
my $pos = 0; |
71 |
|
|
72 |
print "static $path $type $size block: $block\n"; |
warn "static $path $type $size block: $block\n"; |
73 |
|
|
74 |
|
progress_bar::start; |
75 |
|
|
76 |
while( my $len = read $fh, $buff, $block ) { |
while( my $len = read $fh, $buff, $block ) { |
77 |
print $client $buff; |
print $client $buff; |
78 |
|
$client->flush; |
79 |
$pos += $len; |
$pos += $len; |
80 |
printf "%s %d/%d %.2f%% %.2f K/s\r" |
progress_bar::tick( $path, $pos, $size ); |
|
, $path, $pos |
|
|
, $size, $pos * 100 / $size |
|
|
, ( $pos / 1024 ) / ( time() - $start_t ) |
|
|
; |
|
81 |
} |
} |
82 |
close($fh); |
close($fh); |
83 |
close($client); |
close($client); |
84 |
|
|
85 |
print "\n"; |
print STDERR "\n"; |
86 |
|
|
87 |
exit; |
warn "exit static child"; |
88 |
|
|
89 |
|
exit(0); |
90 |
} |
} |
91 |
|
|
92 |
use boolean; |
use boolean; |
98 |
|
|
99 |
sub start_stop { |
sub start_stop { |
100 |
my $daemon = shift; |
my $daemon = shift; |
101 |
my $pid = $pids->{$daemon}; |
my $pid = $pids->{$daemon} || 'not started'; |
102 |
|
|
103 |
warn "start_stop $daemon $pid\n"; |
warn "start_stop $daemon $pid\n"; |
104 |
|
|
136 |
sub get_request { |
sub get_request { |
137 |
my ( $client, $path, $param ) = @_; |
my ( $client, $path, $param ) = @_; |
138 |
|
|
139 |
|
server->refresh; |
140 |
|
|
141 |
warn "get_request $path ", $param ? dump( $param ) : '', "\n"; |
warn "get_request $path ", $param ? dump( $param ) : '', "\n"; |
142 |
|
|
143 |
if ( my $found = static( $client,$path ) ) { |
if ( my $found = static( $client,$path ) ) { |
242 |
} |
} |
243 |
|
|
244 |
use browser; |
use browser; |
245 |
|
use network; |
246 |
|
|
247 |
sub start { |
sub start { |
248 |
|
|
249 |
|
warn 'tap ', network::tap(); |
250 |
|
|
251 |
my $server = IO::Socket::INET->new( |
my $server = IO::Socket::INET->new( |
252 |
Proto => 'tcp', |
Proto => 'tcp', |
253 |
LocalPort => $httpd::port, |
LocalPort => $httpd::port, |
261 |
start_stop 'screen'; |
start_stop 'screen'; |
262 |
start_stop 'kvm'; |
start_stop 'kvm'; |
263 |
|
|
264 |
while (my $client = $server->accept()) { |
while (1) { |
265 |
$client->autoflush(1); |
my $client = $server->accept() || next; # ALARM trickle us |
266 |
my $request = <$client>; |
my $request = <$client>; |
267 |
|
|
268 |
warn "request $request\n" if $debug; |
warn "request $request\n" if $debug; |
269 |
|
|
|
Module::Refresh->refresh; |
|
|
|
|
270 |
if ($request =~ m{^GET (/.*) HTTP/1.[01]}) { |
if ($request =~ m{^GET (/.*) HTTP/1.[01]}) { |
271 |
my $path = $1; |
my $path = $1; |
272 |
my $param; |
my $param; |