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