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.1 |
sub handle_req { |
35 |
|
|
my ($self) = @_; |
36 |
|
|
|
37 |
root |
1.4 |
while ($self->{rbuf} =~ s/^( ( [^\015]+ | . )+? )\015\012\015\012//xs) { |
38 |
|
|
my $req = $1; |
39 |
root |
1.1 |
|
40 |
root |
1.4 |
# we ignore headers atm. |
41 |
root |
1.1 |
|
42 |
root |
1.4 |
$req =~ s%^GET (\S+) HTTP/[0-9.]+\015\012%% |
43 |
|
|
or return $self->fatal; |
44 |
root |
1.1 |
|
45 |
root |
1.4 |
my $uri = $1; |
46 |
root |
1.1 |
|
47 |
root |
1.4 |
$uri =~ s%^http://[^/]*%%i; # just in case |
48 |
root |
1.1 |
|
49 |
root |
1.4 |
cf::debug "HTTP GET: $self->{id} $uri"; |
50 |
root |
1.2 |
|
51 |
root |
1.4 |
if ($uri =~ m%^/([0-9a-f]+)$%) { # faces |
52 |
|
|
my $idx = $cf::FACEHASH{pack "H*", $1}; |
53 |
root |
1.2 |
|
54 |
root |
1.4 |
$idx |
55 |
root |
1.7 |
or do { $self->respond ("404 illegal face name"), next }; |
56 |
root |
1.1 |
|
57 |
root |
1.4 |
if ($req =~ /if-none-match/i) { # dirtiest hack evar |
58 |
root |
1.6 |
$self->respond ("304 not modified", "", $cache_headers); |
59 |
root |
1.7 |
next; |
60 |
root |
1.4 |
} |
61 |
root |
1.1 |
|
62 |
root |
1.4 |
my $type = cf::face::get_type $idx, 1; |
63 |
root |
1.1 |
|
64 |
root |
1.4 |
if ($type & 1) { |
65 |
|
|
$self->respond ("404 type $type not served yet"); |
66 |
root |
1.1 |
} else { |
67 |
root |
1.4 |
if ($type == 0) { # faces |
68 |
root |
1.6 |
$self->respond ("200 OK", (cf::face::get_data $idx, 1), "content-type: image/png\015\012$cache_headers"); |
69 |
root |
1.4 |
} else { |
70 |
|
|
$self->respond ("404 type $type not served yet"); |
71 |
|
|
} |
72 |
root |
1.1 |
} |
73 |
root |
1.4 |
} elsif ($uri eq "/allimgs") { # for debugging |
74 |
|
|
my $body = "<html><body>"; |
75 |
root |
1.2 |
|
76 |
root |
1.4 |
for (1 .. cf::face::faces_size - 1) { |
77 |
|
|
next if cf::face::get_type $_; |
78 |
|
|
my $name = cf::face::get_chksum $_, 1; |
79 |
root |
1.5 |
$body .= "img src='" . (unpack "H*", $name) . "'<br>"; |
80 |
root |
1.4 |
} |
81 |
root |
1.2 |
|
82 |
root |
1.4 |
$body .= "</body></html>"; |
83 |
root |
1.2 |
|
84 |
root |
1.4 |
$self->respond ("200 OK", $body, "Content-Type: text/html\015\012"); |
85 |
|
|
} else { |
86 |
|
|
$self->respond ("404 not found"); |
87 |
|
|
} |
88 |
root |
1.1 |
} |
89 |
|
|
} |
90 |
|
|
|
91 |
|
|
# dirty hack: called directly from tcp.ext |
92 |
|
|
sub server { |
93 |
|
|
my $self = bless { |
94 |
root |
1.2 |
id => $_[0], |
95 |
|
|
fh => $_[1], |
96 |
|
|
rbuf => $_[2], |
97 |
root |
1.1 |
wbuf => "", |
98 |
|
|
}; |
99 |
|
|
|
100 |
|
|
$self->{rw} = AE::io $self->{fh}, 0, sub { |
101 |
|
|
my $len = sysread $self->{fh}, $self->{rbuf}, 4096, length $self->{rbuf}; |
102 |
|
|
|
103 |
|
|
if ($len == 0) { |
104 |
|
|
delete $self->{rw}; |
105 |
|
|
} else { |
106 |
|
|
$self->handle_req; |
107 |
|
|
|
108 |
|
|
delete $self->{rw} if length $self->{rbuf} > 8192; # headers too long |
109 |
|
|
} |
110 |
|
|
}; |
111 |
|
|
|
112 |
|
|
$self->handle_req; # in the unlikely case of the buffer already forming a valid request |
113 |
|
|
} |
114 |
|
|
|
115 |
|
|
cf::register_exticmd http_faceurl => sub { |
116 |
|
|
my ($ns) = @_; |
117 |
|
|
|
118 |
|
|
"/" |
119 |
|
|
}; |
120 |
|
|
|