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