1 |
root |
1.1 |
#! perl # optional depends=tcp |
2 |
|
|
|
3 |
root |
1.9 |
# http server - this tried to speak enough of http 1.1 (and 1.0) |
4 |
|
|
# to work with browsers. it does not even attempt to be a complete |
5 |
|
|
# implementation, although it should be mostly correct for that it does. |
6 |
root |
1.1 |
|
7 |
|
|
sub send { |
8 |
|
|
my $self = $_[0]; |
9 |
|
|
|
10 |
root |
1.9 |
if (length $self->{wbuf}) { |
11 |
|
|
$self->{wbuf} .= $_[1]; |
12 |
|
|
} else { |
13 |
|
|
$self->{wbuf} .= $_[1]; |
14 |
root |
1.1 |
|
15 |
|
|
my $len = syswrite $self->{fh}, $self->{wbuf}; |
16 |
|
|
substr $self->{wbuf}, 0, $len, ""; |
17 |
|
|
|
18 |
root |
1.9 |
$self->{ww} = AE::io $self->{fh}, 1, sub { |
19 |
|
|
my $len = syswrite $self->{fh}, $self->{wbuf}; |
20 |
|
|
substr $self->{wbuf}, 0, $len, ""; |
21 |
|
|
|
22 |
|
|
$self->{ww}->stop unless length $self->{wbuf}; |
23 |
|
|
} if length $self->{wbuf}; |
24 |
|
|
} |
25 |
root |
1.1 |
} |
26 |
|
|
|
27 |
|
|
sub fatal { |
28 |
|
|
my ($self) = @_; |
29 |
|
|
|
30 |
|
|
$self->send ("HTTP/1.1 500 internal error\015\012"); |
31 |
|
|
} |
32 |
|
|
|
33 |
|
|
sub respond { |
34 |
|
|
$_[0]->send ("HTTP/1.1 $_[1]\015\012" |
35 |
|
|
. "content-length: " . (0 + length $_[2]) . "\015\012" |
36 |
|
|
. "access-control-allow-origin: *\015\012" |
37 |
root |
1.6 |
. $_[0]{ohdr} |
38 |
root |
1.9 |
. "$_[3]\015\012" . ($_[0]{give_head} ? "" : $_[2])); |
39 |
root |
1.1 |
} |
40 |
|
|
|
41 |
|
|
my $cache_headers = "cache-control: max-age=8640000\015\012" |
42 |
|
|
. "etag: \"0\"\015\012"; |
43 |
|
|
|
44 |
|
|
sub content_type { |
45 |
|
|
return "content-type: image/png\015\012" if $_[0] =~ /^\x89PNG/; |
46 |
|
|
return "content-type: image/jpeg\015\012" if $_[0] =~ /^......JFIF/s; |
47 |
|
|
return "content-type: audio/wav\015\012" if $_[0] =~ /^RIFF/; |
48 |
|
|
return "content-type: audio/ogg\015\012" if $_[0] =~ /^OggS/; |
49 |
root |
1.7 |
return "content-type: text/html\015\012" if $_[0] =~ /^</; |
50 |
root |
1.1 |
|
51 |
|
|
"content-type: text/plain\015\012" |
52 |
|
|
} |
53 |
|
|
|
54 |
root |
1.9 |
sub handle_con { |
55 |
root |
1.1 |
my ($self) = @_; |
56 |
|
|
|
57 |
root |
1.9 |
my ($method, $uri, $http, $req, $close, $len); |
58 |
root |
1.1 |
|
59 |
root |
1.9 |
while () { |
60 |
|
|
while ($self->{rbuf} =~ s/^( (?: [^\015]+ | . )+? \015\012)\015\012//xs) { |
61 |
|
|
$req = $1; |
62 |
|
|
|
63 |
|
|
# we ignore headers atm. |
64 |
|
|
|
65 |
|
|
$req =~ m%^(\S+) (\S+) HTTP/([0-9.]+)\015\012%ig |
66 |
|
|
or return $self->respond ("400 bad request"); |
67 |
|
|
|
68 |
|
|
$method = uc $1; |
69 |
|
|
$uri = $2; |
70 |
|
|
$http = $3; |
71 |
|
|
|
72 |
|
|
$uri =~ s%^http://[^/]*%%i; # just in case |
73 |
|
|
$uri =~ s/%([0-9a-fA-F][0-9a-fA-F])/chr hex $1/ge; # %-decode |
74 |
|
|
|
75 |
|
|
# my %hdr; |
76 |
|
|
# |
77 |
|
|
# $hdr{lc $1} .= ",$2" |
78 |
|
|
# while $req =~ /\G |
79 |
|
|
# ([^:\000-\037]*): |
80 |
|
|
# [\011\040]* |
81 |
|
|
# ((?: [^\012]+ | \012[\011\040] )*) |
82 |
|
|
# \012 |
83 |
|
|
# /gxc; |
84 |
|
|
# |
85 |
|
|
# $req =~ /\G$/ |
86 |
|
|
# or return $self->respond ("400 bad request"); |
87 |
|
|
# |
88 |
|
|
# # remove the "," prefix we added to all headers above |
89 |
|
|
# substr $_, 0, 1, "" |
90 |
|
|
# for values %hdr; |
91 |
|
|
|
92 |
|
|
if ($http == 1.0) { |
93 |
|
|
if ($req =~ /^connection\s*:\s*keep-alive/mi) { |
94 |
|
|
$self->{ohdr} = "connection: keep-alive\015\012"; |
95 |
|
|
} else { |
96 |
|
|
$self->{ohdr} = "connection: close\015\012"; |
97 |
|
|
$close = 1; |
98 |
|
|
} |
99 |
|
|
} |
100 |
root |
1.1 |
|
101 |
root |
1.9 |
$self->{give_head} = $method eq "HEAD" |
102 |
|
|
and $method = "GET"; |
103 |
root |
1.1 |
|
104 |
root |
1.9 |
cf::debug "HTTP $method: $self->{id} $uri"; |
105 |
root |
1.6 |
|
106 |
root |
1.9 |
if ($method ne "GET") { |
107 |
|
|
$self->respond ("405 no $method"); |
108 |
|
|
next; |
109 |
root |
1.6 |
} |
110 |
root |
1.1 |
|
111 |
root |
1.9 |
# not needed for GET, but who knows |
112 |
|
|
$self->send ("HTTP/1.1 100 go on\015\012") |
113 |
|
|
if $req =~ /^expect:.*\b100-continue\b/i; |
114 |
|
|
|
115 |
|
|
if ($uri =~ m%^/([0-9a-f]+)(M?)$%) { # faces |
116 |
|
|
my $want_meta = $2; |
117 |
|
|
my $idx = $cf::FACEHASH{pack "H*", $1}; |
118 |
|
|
|
119 |
|
|
$idx |
120 |
|
|
or do { $self->respond ("404 illegal face name"), next }; |
121 |
|
|
|
122 |
|
|
if ($req =~ /if-none-match/i) { # dirtiest hack evar |
123 |
|
|
$self->respond ("304 not modified", "", $cache_headers); |
124 |
|
|
next; |
125 |
|
|
} |
126 |
root |
1.1 |
|
127 |
root |
1.9 |
my $type = cf::face::get_type $idx, 1; |
128 |
|
|
my $data = cf::face::get_data $idx, 1; |
129 |
root |
1.1 |
|
130 |
root |
1.9 |
(my $meta, $data) = unpack "(w/a*)*", $data |
131 |
|
|
if $type & 1; |
132 |
root |
1.1 |
|
133 |
root |
1.9 |
if ($want_meta) { |
134 |
|
|
if ($type & 1) { |
135 |
|
|
$self->respond ("200 OK", $meta, "content-type: text/plain\015\012" . $cache_headers); |
136 |
|
|
} else { |
137 |
|
|
$self->respond ("404 type $type has no metadata"); |
138 |
|
|
} |
139 |
|
|
} else { |
140 |
|
|
$self->respond ("200 OK", $data, (content_type $data) . $cache_headers); |
141 |
|
|
} |
142 |
root |
1.1 |
|
143 |
root |
1.9 |
} elsif (my $idx = (cf::face::find "res/http$uri") || (cf::face::find "res/http${uri}index.html")) { |
144 |
|
|
# TODO: use etag (shudder) |
145 |
|
|
my $data = cf::face::get_data $idx, 1; |
146 |
|
|
$self->respond ("200 OK", $data, (content_type $data) . $cache_headers); |
147 |
|
|
|
148 |
|
|
} elsif (cf::face::find "res/http$uri/index.html") { |
149 |
|
|
$self->respond ("302 dirslash", "", "location: $uri/\015\012"); |
150 |
root |
1.1 |
|
151 |
root |
1.9 |
} elsif ($uri eq "/debug") { # for debugging |
152 |
|
|
my @body = "<html><body>"; |
153 |
root |
1.1 |
|
154 |
root |
1.9 |
for my $type (6, 5, 4, 3, 2, 1, 0) { |
155 |
|
|
push @body, "<h1>$type</h1>"; |
156 |
root |
1.1 |
|
157 |
root |
1.9 |
for (1 .. cf::face::faces_size - 1) { |
158 |
|
|
cf::cede_to_tick; |
159 |
|
|
|
160 |
|
|
next if $type != cf::face::get_type $_; |
161 |
|
|
my $name = cf::face::get_name $_; |
162 |
|
|
my $id = unpack "H*", cf::face::get_chksum $_, 1; |
163 |
|
|
push @body, "$_ <a href='$id'>$name ($id)</a>"; |
164 |
|
|
push @body, " <a href='${id}M'>(meta)</a>" if $type & 1; |
165 |
|
|
push @body, "<br>"; |
166 |
|
|
} |
167 |
root |
1.1 |
} |
168 |
root |
1.9 |
|
169 |
|
|
push @body, "</body></html>"; |
170 |
|
|
|
171 |
|
|
my $body = join "", @body; |
172 |
|
|
utf8::encode $body; |
173 |
|
|
$self->respond ("200 OK", $body, "content-type: text/html\015\012"); |
174 |
|
|
|
175 |
root |
1.1 |
} else { |
176 |
root |
1.9 |
$self->respond ("404 not found"); |
177 |
root |
1.1 |
} |
178 |
|
|
|
179 |
root |
1.9 |
cf::cede_to_tick; |
180 |
|
|
} |
181 |
root |
1.1 |
|
182 |
root |
1.9 |
return if $close; # http 1.0 only currently |
183 |
|
|
return if length $self->{rbuf} > 8192; # headers too long |
184 |
root |
1.1 |
|
185 |
root |
1.9 |
Coro::AnyEvent::readable $self->{fh}, 6; |
186 |
root |
1.1 |
|
187 |
root |
1.9 |
$len = sysread $self->{fh}, $self->{rbuf}, 4096, length $self->{rbuf}; |
188 |
root |
1.1 |
|
189 |
root |
1.9 |
return if $len <= 0; |
190 |
|
|
}; |
191 |
root |
1.1 |
} |
192 |
|
|
|
193 |
|
|
our $DETECTOR = ext::tcp::register http => 64, sub { |
194 |
|
|
# regex avoids conflict with websockets, which use /ws |
195 |
root |
1.2 |
m{^(?i:GET|HEAD|OPTIONS) \ (?! (?i:http://[^/]+)? /ws \ ) }x |
196 |
root |
1.1 |
}, sub { |
197 |
|
|
my $self = bless { |
198 |
|
|
id => $_[0], |
199 |
|
|
fh => $_[1], |
200 |
|
|
rbuf => $_[2], |
201 |
|
|
wbuf => "", |
202 |
|
|
}; |
203 |
|
|
|
204 |
root |
1.9 |
$self->{async} = Coro::async_pool { |
205 |
|
|
$Coro::current->nice (4); |
206 |
|
|
$Coro::current->{desc} = "http $self->{id}"; |
207 |
root |
1.1 |
|
208 |
root |
1.9 |
$self->handle_con; |
209 |
root |
1.1 |
|
210 |
root |
1.9 |
# this is ultra-ugly, but overall, fast |
211 |
|
|
# Coro::AnyEvent::writable $self->{fh} |
212 |
|
|
# while length $self->{wbuf}; |
213 |
|
|
|
214 |
|
|
# %$self = (); |
215 |
root |
1.1 |
}; |
216 |
|
|
}; |
217 |
|
|
|
218 |
|
|
cf::register_exticmd http_faceurl => sub { |
219 |
|
|
my ($ns) = @_; |
220 |
|
|
|
221 |
|
|
"/" |
222 |
|
|
}; |
223 |
|
|
|