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