| 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 |
root |
1.6 |
$_[0]->send ("HTTP/1.1 $_[1]\015\012content-length: " . (0 + length $_[2]) . "\015\012$_[3]\015\012$_[2]"); |
| 29 |
root |
1.1 |
} |
| 30 |
|
|
|
| 31 |
root |
1.6 |
my $cache_headers = "cache-control: max-age=8640000\015\012" |
| 32 |
|
|
. "etag: \"0\"\015\012"; |
| 33 |
|
|
|
| 34 |
root |
1.8 |
sub content_type { |
| 35 |
|
|
return "content-type: image/png\015\012" if $_[0] =~ /^\x89PNG/; |
| 36 |
|
|
return "content-type: image/jpeg\015\012" if $_[0] =~ /^......JFIF/s; |
| 37 |
|
|
return "content-type: audio/wav\015\012" if $_[0] =~ /^RIFF/; |
| 38 |
|
|
return "content-type: audio/x-ogg\015\012" if $_[0] =~ /^OggS/; |
| 39 |
|
|
|
| 40 |
|
|
"content-type: text/plain\015\012" |
| 41 |
|
|
} |
| 42 |
|
|
|
| 43 |
root |
1.1 |
sub handle_req { |
| 44 |
|
|
my ($self) = @_; |
| 45 |
|
|
|
| 46 |
root |
1.4 |
while ($self->{rbuf} =~ s/^( ( [^\015]+ | . )+? )\015\012\015\012//xs) { |
| 47 |
|
|
my $req = $1; |
| 48 |
root |
1.1 |
|
| 49 |
root |
1.4 |
# we ignore headers atm. |
| 50 |
root |
1.1 |
|
| 51 |
root |
1.4 |
$req =~ s%^GET (\S+) HTTP/[0-9.]+\015\012%% |
| 52 |
|
|
or return $self->fatal; |
| 53 |
root |
1.1 |
|
| 54 |
root |
1.4 |
my $uri = $1; |
| 55 |
root |
1.1 |
|
| 56 |
root |
1.4 |
$uri =~ s%^http://[^/]*%%i; # just in case |
| 57 |
root |
1.1 |
|
| 58 |
root |
1.4 |
cf::debug "HTTP GET: $self->{id} $uri"; |
| 59 |
root |
1.2 |
|
| 60 |
root |
1.8 |
if ($uri =~ m%^/(M?)([0-9a-f]+)$%) { # faces |
| 61 |
|
|
my $want_meta = $1; |
| 62 |
|
|
my $idx = $cf::FACEHASH{pack "H*", $2}; |
| 63 |
root |
1.2 |
|
| 64 |
root |
1.4 |
$idx |
| 65 |
root |
1.7 |
or do { $self->respond ("404 illegal face name"), next }; |
| 66 |
root |
1.1 |
|
| 67 |
root |
1.4 |
if ($req =~ /if-none-match/i) { # dirtiest hack evar |
| 68 |
root |
1.6 |
$self->respond ("304 not modified", "", $cache_headers); |
| 69 |
root |
1.7 |
next; |
| 70 |
root |
1.4 |
} |
| 71 |
root |
1.1 |
|
| 72 |
root |
1.4 |
my $type = cf::face::get_type $idx, 1; |
| 73 |
root |
1.8 |
my $data = cf::face::get_data $idx, 1; |
| 74 |
root |
1.1 |
|
| 75 |
root |
1.8 |
(my $meta, $data) = unpack "(w/a*)*", $data |
| 76 |
|
|
if $type & 1; |
| 77 |
|
|
|
| 78 |
|
|
if ($want_meta) { |
| 79 |
|
|
if ($type & 1) { |
| 80 |
|
|
$self->respond ("200 OK", $meta, "content-type: text/plain\015\012" . $cache_headers); |
| 81 |
root |
1.4 |
} else { |
| 82 |
root |
1.8 |
$self->respond ("404 type $type has no metadata"); |
| 83 |
root |
1.4 |
} |
| 84 |
root |
1.8 |
} else { |
| 85 |
|
|
$self->respond ("200 OK", $data, (content_type $data) . $cache_headers); |
| 86 |
root |
1.1 |
} |
| 87 |
root |
1.8 |
|
| 88 |
|
|
} elsif ($uri eq "/debug") { # for debugging |
| 89 |
root |
1.4 |
my $body = "<html><body>"; |
| 90 |
root |
1.2 |
|
| 91 |
root |
1.8 |
for my $type (6, 5, 4, 3, 2, 1, 0) { |
| 92 |
|
|
$body .= "<h1>$type</h1>"; |
| 93 |
|
|
|
| 94 |
|
|
for (1 .. cf::face::faces_size - 1) { |
| 95 |
|
|
next if $type != cf::face::get_type $_; |
| 96 |
|
|
my $name = cf::face::get_name $_; |
| 97 |
|
|
my $id = unpack "H*", cf::face::get_chksum $_, 1; |
| 98 |
|
|
$body .= "<a href='$id'>$name ($id)</a>"; |
| 99 |
|
|
$body .= " <a href='M$id'>(meta)</a>" if $type & 1; |
| 100 |
|
|
$body .= "<br>"; |
| 101 |
|
|
} |
| 102 |
root |
1.4 |
} |
| 103 |
root |
1.2 |
|
| 104 |
root |
1.4 |
$body .= "</body></html>"; |
| 105 |
root |
1.2 |
|
| 106 |
root |
1.4 |
$self->respond ("200 OK", $body, "Content-Type: text/html\015\012"); |
| 107 |
|
|
} else { |
| 108 |
|
|
$self->respond ("404 not found"); |
| 109 |
|
|
} |
| 110 |
root |
1.1 |
} |
| 111 |
|
|
} |
| 112 |
|
|
|
| 113 |
|
|
# dirty hack: called directly from tcp.ext |
| 114 |
|
|
sub server { |
| 115 |
|
|
my $self = bless { |
| 116 |
root |
1.2 |
id => $_[0], |
| 117 |
|
|
fh => $_[1], |
| 118 |
|
|
rbuf => $_[2], |
| 119 |
root |
1.1 |
wbuf => "", |
| 120 |
|
|
}; |
| 121 |
|
|
|
| 122 |
|
|
$self->{rw} = AE::io $self->{fh}, 0, sub { |
| 123 |
|
|
my $len = sysread $self->{fh}, $self->{rbuf}, 4096, length $self->{rbuf}; |
| 124 |
|
|
|
| 125 |
|
|
if ($len == 0) { |
| 126 |
|
|
delete $self->{rw}; |
| 127 |
|
|
} else { |
| 128 |
|
|
$self->handle_req; |
| 129 |
|
|
|
| 130 |
|
|
delete $self->{rw} if length $self->{rbuf} > 8192; # headers too long |
| 131 |
|
|
} |
| 132 |
|
|
}; |
| 133 |
|
|
|
| 134 |
|
|
$self->handle_req; # in the unlikely case of the buffer already forming a valid request |
| 135 |
|
|
} |
| 136 |
|
|
|
| 137 |
|
|
cf::register_exticmd http_faceurl => sub { |
| 138 |
|
|
my ($ns) = @_; |
| 139 |
|
|
|
| 140 |
|
|
"/" |
| 141 |
|
|
}; |
| 142 |
|
|
|