| 1 |
root |
1.1 |
#! perl # mandatory |
| 2 |
|
|
|
| 3 |
|
|
# http server on base port |
| 4 |
|
|
|
| 5 |
|
|
use Coro::AnyEvent; |
| 6 |
|
|
|
| 7 |
root |
1.9 |
our @WEBSOCKETS_FORWARDER = qw(slashflash.pl 13327); |
| 8 |
|
|
|
| 9 |
root |
1.1 |
sub send { |
| 10 |
|
|
my $self = $_[0]; |
| 11 |
|
|
|
| 12 |
|
|
$self->{wbuf} .= $_[1]; |
| 13 |
|
|
|
| 14 |
|
|
$self->{ww} ||= AE::io $self->{fh}, 1, sub { |
| 15 |
|
|
my $len = syswrite $self->{fh}, $self->{wbuf}; |
| 16 |
|
|
substr $self->{wbuf}, 0, $len, ""; |
| 17 |
|
|
|
| 18 |
|
|
delete $self->{ww} unless length $self->{wbuf}; |
| 19 |
|
|
}; |
| 20 |
|
|
} |
| 21 |
|
|
|
| 22 |
|
|
sub fatal { |
| 23 |
|
|
my ($self) = @_; |
| 24 |
|
|
|
| 25 |
|
|
$self->send ("HTTP/1.1 500 internal error\015\012"); |
| 26 |
|
|
delete $self->{rw}; |
| 27 |
|
|
} |
| 28 |
|
|
|
| 29 |
|
|
sub respond { |
| 30 |
root |
1.11 |
$_[0]->send ("HTTP/1.1 $_[1]\015\012" |
| 31 |
|
|
. "content-length: " . (0 + length $_[2]) . "\015\012" |
| 32 |
|
|
. "access-control-allow-origin: *\015\012" |
| 33 |
|
|
. "$_[3]\015\012$_[2]"); |
| 34 |
root |
1.1 |
} |
| 35 |
|
|
|
| 36 |
root |
1.6 |
my $cache_headers = "cache-control: max-age=8640000\015\012" |
| 37 |
|
|
. "etag: \"0\"\015\012"; |
| 38 |
|
|
|
| 39 |
root |
1.8 |
sub content_type { |
| 40 |
|
|
return "content-type: image/png\015\012" if $_[0] =~ /^\x89PNG/; |
| 41 |
|
|
return "content-type: image/jpeg\015\012" if $_[0] =~ /^......JFIF/s; |
| 42 |
|
|
return "content-type: audio/wav\015\012" if $_[0] =~ /^RIFF/; |
| 43 |
|
|
return "content-type: audio/x-ogg\015\012" if $_[0] =~ /^OggS/; |
| 44 |
|
|
|
| 45 |
|
|
"content-type: text/plain\015\012" |
| 46 |
|
|
} |
| 47 |
|
|
|
| 48 |
root |
1.9 |
sub copy { |
| 49 |
|
|
my ($a, $b, $buf) = @_; |
| 50 |
|
|
|
| 51 |
|
|
# ultra-lame |
| 52 |
|
|
Coro::async { |
| 53 |
|
|
while () { |
| 54 |
|
|
while (length $buf) { |
| 55 |
|
|
my $len = syswrite $b, $buf |
| 56 |
|
|
or $! == Errno::EINTR |
| 57 |
|
|
or return; |
| 58 |
|
|
|
| 59 |
|
|
substr $buf, 0, $len, ""; |
| 60 |
|
|
|
| 61 |
|
|
Coro::AnyEvent::writable $b; |
| 62 |
|
|
} |
| 63 |
|
|
|
| 64 |
|
|
Coro::AnyEvent::readable $a; |
| 65 |
|
|
sysread $a, $buf, 4096 |
| 66 |
|
|
or return; |
| 67 |
|
|
} |
| 68 |
|
|
}; |
| 69 |
|
|
} |
| 70 |
|
|
|
| 71 |
root |
1.1 |
sub handle_req { |
| 72 |
|
|
my ($self) = @_; |
| 73 |
|
|
|
| 74 |
root |
1.9 |
while ($self->{rbuf} =~ s/^( (?: [^\015]+ | . )+? )\015\012\015\012//xs) { |
| 75 |
root |
1.4 |
my $req = $1; |
| 76 |
root |
1.1 |
|
| 77 |
root |
1.4 |
# we ignore headers atm. |
| 78 |
root |
1.1 |
|
| 79 |
root |
1.10 |
$req =~ m%^GET (\S+) HTTP/[0-9.]+\015\012% |
| 80 |
root |
1.4 |
or return $self->fatal; |
| 81 |
root |
1.1 |
|
| 82 |
root |
1.4 |
my $uri = $1; |
| 83 |
root |
1.1 |
|
| 84 |
root |
1.4 |
$uri =~ s%^http://[^/]*%%i; # just in case |
| 85 |
root |
1.1 |
|
| 86 |
root |
1.4 |
cf::debug "HTTP GET: $self->{id} $uri"; |
| 87 |
root |
1.2 |
|
| 88 |
root |
1.8 |
if ($uri =~ m%^/(M?)([0-9a-f]+)$%) { # faces |
| 89 |
|
|
my $want_meta = $1; |
| 90 |
|
|
my $idx = $cf::FACEHASH{pack "H*", $2}; |
| 91 |
root |
1.2 |
|
| 92 |
root |
1.4 |
$idx |
| 93 |
root |
1.7 |
or do { $self->respond ("404 illegal face name"), next }; |
| 94 |
root |
1.1 |
|
| 95 |
root |
1.4 |
if ($req =~ /if-none-match/i) { # dirtiest hack evar |
| 96 |
root |
1.6 |
$self->respond ("304 not modified", "", $cache_headers); |
| 97 |
root |
1.7 |
next; |
| 98 |
root |
1.4 |
} |
| 99 |
root |
1.1 |
|
| 100 |
root |
1.4 |
my $type = cf::face::get_type $idx, 1; |
| 101 |
root |
1.8 |
my $data = cf::face::get_data $idx, 1; |
| 102 |
root |
1.1 |
|
| 103 |
root |
1.8 |
(my $meta, $data) = unpack "(w/a*)*", $data |
| 104 |
|
|
if $type & 1; |
| 105 |
|
|
|
| 106 |
|
|
if ($want_meta) { |
| 107 |
|
|
if ($type & 1) { |
| 108 |
|
|
$self->respond ("200 OK", $meta, "content-type: text/plain\015\012" . $cache_headers); |
| 109 |
root |
1.4 |
} else { |
| 110 |
root |
1.8 |
$self->respond ("404 type $type has no metadata"); |
| 111 |
root |
1.4 |
} |
| 112 |
root |
1.8 |
} else { |
| 113 |
|
|
$self->respond ("200 OK", $data, (content_type $data) . $cache_headers); |
| 114 |
root |
1.1 |
} |
| 115 |
root |
1.8 |
|
| 116 |
|
|
} elsif ($uri eq "/debug") { # for debugging |
| 117 |
root |
1.4 |
my $body = "<html><body>"; |
| 118 |
root |
1.2 |
|
| 119 |
root |
1.8 |
for my $type (6, 5, 4, 3, 2, 1, 0) { |
| 120 |
|
|
$body .= "<h1>$type</h1>"; |
| 121 |
|
|
|
| 122 |
|
|
for (1 .. cf::face::faces_size - 1) { |
| 123 |
|
|
next if $type != cf::face::get_type $_; |
| 124 |
|
|
my $name = cf::face::get_name $_; |
| 125 |
|
|
my $id = unpack "H*", cf::face::get_chksum $_, 1; |
| 126 |
root |
1.9 |
$body .= "$_ <a href='$id'>$name ($id)</a>"; |
| 127 |
root |
1.8 |
$body .= " <a href='M$id'>(meta)</a>" if $type & 1; |
| 128 |
|
|
$body .= "<br>"; |
| 129 |
|
|
} |
| 130 |
root |
1.4 |
} |
| 131 |
root |
1.2 |
|
| 132 |
root |
1.4 |
$body .= "</body></html>"; |
| 133 |
root |
1.2 |
|
| 134 |
root |
1.4 |
$self->respond ("200 OK", $body, "Content-Type: text/html\015\012"); |
| 135 |
root |
1.9 |
} elsif ($uri eq "/ws") { |
| 136 |
|
|
my $fh = $self->{fh}; |
| 137 |
root |
1.10 |
my $buf = "$req\015\012\015\012$self->{rbuf}"; |
| 138 |
root |
1.9 |
|
| 139 |
|
|
%$self = (); |
| 140 |
|
|
|
| 141 |
|
|
&AnyEvent::Socket::tcp_connect (@WEBSOCKETS_FORWARDER, sub { |
| 142 |
|
|
my ($fh2) = shift; |
| 143 |
|
|
|
| 144 |
|
|
Coro::async { |
| 145 |
|
|
if ($fh2) { |
| 146 |
|
|
my $a = copy $fh, $fh2, $buf; |
| 147 |
|
|
my $b = copy $fh2, $fh; |
| 148 |
|
|
|
| 149 |
|
|
$a->join; |
| 150 |
|
|
$b->join; |
| 151 |
|
|
|
| 152 |
|
|
close $fh2; |
| 153 |
|
|
} |
| 154 |
|
|
|
| 155 |
|
|
close $fh; |
| 156 |
|
|
}; |
| 157 |
|
|
}); |
| 158 |
|
|
|
| 159 |
root |
1.4 |
} else { |
| 160 |
|
|
$self->respond ("404 not found"); |
| 161 |
|
|
} |
| 162 |
root |
1.1 |
} |
| 163 |
|
|
} |
| 164 |
|
|
|
| 165 |
|
|
# dirty hack: called directly from tcp.ext |
| 166 |
|
|
sub server { |
| 167 |
|
|
my $self = bless { |
| 168 |
root |
1.2 |
id => $_[0], |
| 169 |
|
|
fh => $_[1], |
| 170 |
|
|
rbuf => $_[2], |
| 171 |
root |
1.1 |
wbuf => "", |
| 172 |
|
|
}; |
| 173 |
|
|
|
| 174 |
|
|
$self->{rw} = AE::io $self->{fh}, 0, sub { |
| 175 |
|
|
my $len = sysread $self->{fh}, $self->{rbuf}, 4096, length $self->{rbuf}; |
| 176 |
|
|
|
| 177 |
|
|
if ($len == 0) { |
| 178 |
|
|
delete $self->{rw}; |
| 179 |
|
|
} else { |
| 180 |
|
|
$self->handle_req; |
| 181 |
|
|
|
| 182 |
|
|
delete $self->{rw} if length $self->{rbuf} > 8192; # headers too long |
| 183 |
|
|
} |
| 184 |
|
|
}; |
| 185 |
|
|
|
| 186 |
|
|
$self->handle_req; # in the unlikely case of the buffer already forming a valid request |
| 187 |
|
|
} |
| 188 |
|
|
|
| 189 |
|
|
cf::register_exticmd http_faceurl => sub { |
| 190 |
|
|
my ($ns) = @_; |
| 191 |
|
|
|
| 192 |
|
|
"/" |
| 193 |
|
|
}; |
| 194 |
|
|
|