| 1 |
root |
1.1 |
#! perl # mandatory |
| 2 |
|
|
|
| 3 |
|
|
# http server on base port |
| 4 |
|
|
|
| 5 |
|
|
use Coro::AnyEvent; |
| 6 |
|
|
|
| 7 |
|
|
sub send { |
| 8 |
|
|
my $self = $_[0]; |
| 9 |
|
|
|
| 10 |
|
|
$self->{wbuf} .= $_[1]; |
| 11 |
|
|
|
| 12 |
|
|
$self->{ww} ||= AE::io $self->{fh}, 1, sub { |
| 13 |
|
|
my $len = syswrite $self->{fh}, $self->{wbuf}; |
| 14 |
|
|
substr $self->{wbuf}, 0, $len, ""; |
| 15 |
|
|
|
| 16 |
|
|
delete $self->{ww} unless length $self->{wbuf}; |
| 17 |
|
|
}; |
| 18 |
|
|
} |
| 19 |
|
|
|
| 20 |
|
|
sub fatal { |
| 21 |
|
|
my ($self) = @_; |
| 22 |
|
|
|
| 23 |
|
|
$self->send ("HTTP/1.1 500 internal error\015\012"); |
| 24 |
|
|
delete $self->{rw}; |
| 25 |
|
|
} |
| 26 |
|
|
|
| 27 |
|
|
sub respond { |
| 28 |
|
|
$_[0]->send ("HTTP/1.1 $_[1]\015\012Content-Length: " . (0 + length $_[2]) . "\015\012$_[3]\015\012$_[2]"); |
| 29 |
|
|
} |
| 30 |
|
|
|
| 31 |
|
|
sub handle_req { |
| 32 |
|
|
my ($self) = @_; |
| 33 |
|
|
|
| 34 |
root |
1.4 |
while ($self->{rbuf} =~ s/^( ( [^\015]+ | . )+? )\015\012\015\012//xs) { |
| 35 |
|
|
my $req = $1; |
| 36 |
root |
1.1 |
|
| 37 |
root |
1.4 |
# we ignore headers atm. |
| 38 |
root |
1.1 |
|
| 39 |
root |
1.4 |
$req =~ s%^GET (\S+) HTTP/[0-9.]+\015\012%% |
| 40 |
|
|
or return $self->fatal; |
| 41 |
root |
1.1 |
|
| 42 |
root |
1.4 |
my $uri = $1; |
| 43 |
root |
1.1 |
|
| 44 |
root |
1.4 |
$uri =~ s%^http://[^/]*%%i; # just in case |
| 45 |
root |
1.1 |
|
| 46 |
root |
1.4 |
cf::debug "HTTP GET: $self->{id} $uri"; |
| 47 |
root |
1.2 |
|
| 48 |
root |
1.4 |
if ($uri =~ m%^/([0-9a-f]+)$%) { # faces |
| 49 |
|
|
my $idx = $cf::FACEHASH{pack "H*", $1}; |
| 50 |
root |
1.2 |
|
| 51 |
root |
1.4 |
$idx |
| 52 |
|
|
or return $self->respond ("404 illegal face name"); |
| 53 |
root |
1.1 |
|
| 54 |
root |
1.4 |
if ($req =~ /if-none-match/i) { # dirtiest hack evar |
| 55 |
|
|
$self->respond ("304 not modified"); |
| 56 |
|
|
return; |
| 57 |
|
|
} |
| 58 |
root |
1.1 |
|
| 59 |
root |
1.4 |
my $type = cf::face::get_type $idx, 1; |
| 60 |
root |
1.1 |
|
| 61 |
root |
1.4 |
if ($type & 1) { |
| 62 |
|
|
$self->respond ("404 type $type not served yet"); |
| 63 |
root |
1.1 |
} else { |
| 64 |
root |
1.4 |
if ($type == 0) { # faces |
| 65 |
|
|
$self->respond ("200 OK", (cf::face::get_data $idx, 1), |
| 66 |
|
|
"content-type: image/png\015\012" |
| 67 |
|
|
. "cache-control: max-age=864000\015\012" |
| 68 |
|
|
. "etag: \"0\"\015\012" |
| 69 |
|
|
); |
| 70 |
|
|
} else { |
| 71 |
|
|
$self->respond ("404 type $type not served yet"); |
| 72 |
|
|
} |
| 73 |
root |
1.1 |
} |
| 74 |
root |
1.4 |
} elsif ($uri eq "/allimgs") { # for debugging |
| 75 |
|
|
my $body = "<html><body>"; |
| 76 |
root |
1.2 |
|
| 77 |
root |
1.4 |
for (1 .. cf::face::faces_size - 1) { |
| 78 |
|
|
next if cf::face::get_type $_; |
| 79 |
|
|
my $name = cf::face::get_chksum $_, 1; |
| 80 |
|
|
$body .= "<img src='" . (unpack "H*", $name) . "'><br>"; |
| 81 |
|
|
} |
| 82 |
root |
1.2 |
|
| 83 |
root |
1.4 |
$body .= "</body></html>"; |
| 84 |
root |
1.2 |
|
| 85 |
root |
1.4 |
$self->respond ("200 OK", $body, "Content-Type: text/html\015\012"); |
| 86 |
|
|
} else { |
| 87 |
|
|
$self->respond ("404 not found"); |
| 88 |
|
|
} |
| 89 |
root |
1.1 |
} |
| 90 |
|
|
} |
| 91 |
|
|
|
| 92 |
|
|
# dirty hack: called directly from tcp.ext |
| 93 |
|
|
sub server { |
| 94 |
|
|
my $self = bless { |
| 95 |
root |
1.2 |
id => $_[0], |
| 96 |
|
|
fh => $_[1], |
| 97 |
|
|
rbuf => $_[2], |
| 98 |
root |
1.1 |
wbuf => "", |
| 99 |
|
|
}; |
| 100 |
|
|
|
| 101 |
|
|
$self->{rw} = AE::io $self->{fh}, 0, sub { |
| 102 |
|
|
my $len = sysread $self->{fh}, $self->{rbuf}, 4096, length $self->{rbuf}; |
| 103 |
|
|
|
| 104 |
|
|
if ($len == 0) { |
| 105 |
|
|
delete $self->{rw}; |
| 106 |
|
|
} else { |
| 107 |
|
|
$self->handle_req; |
| 108 |
|
|
|
| 109 |
|
|
delete $self->{rw} if length $self->{rbuf} > 8192; # headers too long |
| 110 |
|
|
} |
| 111 |
|
|
}; |
| 112 |
|
|
|
| 113 |
|
|
$self->handle_req; # in the unlikely case of the buffer already forming a valid request |
| 114 |
|
|
} |
| 115 |
|
|
|
| 116 |
|
|
cf::register_exticmd http_faceurl => sub { |
| 117 |
|
|
my ($ns) = @_; |
| 118 |
|
|
|
| 119 |
|
|
"/" |
| 120 |
|
|
}; |
| 121 |
|
|
|