1 | #! perl # optional depends=tcp |
1 | #! perl # optional depends=tcp |
2 | |
2 | |
3 | # http server - this tried to speak enough of http 1.1 (and 1.0) |
3 | # http server - this tries to speak enough of http 1.1 (and 1.0) |
4 | # to work with browsers. it does not even attempt to be a complete |
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. |
5 | # implementation, although it should be mostly correct for that it does. |
6 | |
6 | |
7 | sub send { |
7 | sub send { |
8 | my $self = $_[0]; |
8 | my $self = $_[0]; |
… | |
… | |
17 | |
17 | |
18 | $self->{ww} = AE::io $self->{fh}, 1, sub { |
18 | $self->{ww} = AE::io $self->{fh}, 1, sub { |
19 | my $len = syswrite $self->{fh}, $self->{wbuf}; |
19 | my $len = syswrite $self->{fh}, $self->{wbuf}; |
20 | substr $self->{wbuf}, 0, $len, ""; |
20 | substr $self->{wbuf}, 0, $len, ""; |
21 | |
21 | |
|
|
22 | delete $self->{ww} unless $len; # in case of errors, stop |
22 | $self->{ww}->stop unless length $self->{wbuf}; |
23 | delete $self->{ww} unless length $self->{wbuf}; |
23 | } if length $self->{wbuf}; |
24 | } if length $self->{wbuf}; |
24 | } |
25 | } |
25 | } |
26 | } |
26 | |
27 | |
27 | sub fatal { |
28 | sub fatal { |
… | |
… | |
39 | } |
40 | } |
40 | |
41 | |
41 | my $cache_headers = "cache-control: max-age=8640000\015\012" |
42 | my $cache_headers = "cache-control: max-age=8640000\015\012" |
42 | . "etag: \"0\"\015\012"; |
43 | . "etag: \"0\"\015\012"; |
43 | |
44 | |
44 | sub content_type { |
45 | sub content_type($$) { |
45 | return "content-type: image/png\015\012" if $_[0] =~ /^\x89PNG/; |
46 | return "content-type: image/png\015\012" if $_[1] =~ /^\x89PNG/; |
46 | return "content-type: image/jpeg\015\012" if $_[0] =~ /^......JFIF/s; |
47 | return "content-type: image/jpeg\015\012" if $_[1] =~ /^......JFIF/s; |
47 | return "content-type: audio/wav\015\012" if $_[0] =~ /^RIFF/; |
48 | return "content-type: audio/wav\015\012" if $_[1] =~ /^RIFF/; |
48 | return "content-type: audio/ogg\015\012" if $_[0] =~ /^OggS/; |
49 | return "content-type: audio/ogg\015\012" if $_[1] =~ /^OggS/; |
|
|
50 | return "content-type: text/css; charset=utf-8\015\012" if $_[0] =~ /\.css$/; |
|
|
51 | return "content-type: application/javascript; charset=utf-8\015\012" if $_[0] =~ /\.js$/; |
49 | return "content-type: text/html\015\012" if $_[0] =~ /^</; |
52 | return "content-type: text/html; charset=utf-8\015\012" if $_[1] =~ /^</; |
50 | |
53 | |
51 | "content-type: text/plain\015\012" |
54 | "content-type: text/plain; charset=utf-8\015\012" |
52 | } |
55 | } |
53 | |
56 | |
54 | sub handle_con { |
57 | sub handle_con { |
55 | my ($self) = @_; |
58 | my ($self) = @_; |
56 | |
59 | |
… | |
… | |
70 | $http = $3; |
73 | $http = $3; |
71 | |
74 | |
72 | $uri =~ s%^http://[^/]*%%i; # just in case |
75 | $uri =~ s%^http://[^/]*%%i; # just in case |
73 | $uri =~ s/%([0-9a-fA-F][0-9a-fA-F])/chr hex $1/ge; # %-decode |
76 | $uri =~ s/%([0-9a-fA-F][0-9a-fA-F])/chr hex $1/ge; # %-decode |
74 | |
77 | |
|
|
78 | if (0) { |
75 | # my %hdr; |
79 | my %hdr; |
76 | # |
80 | |
77 | # $hdr{lc $1} .= ",$2" |
81 | $hdr{lc $1} .= ",$2" |
78 | # while $req =~ /\G |
82 | while $req =~ /\G |
79 | # ([^:\000-\037]*): |
83 | ([^:\000-\037]*): |
80 | # [\011\040]* |
84 | [\011\040]* |
81 | # ((?: [^\012]+ | \012[\011\040] )*) |
85 | ((?: [^\012]+ | \012[\011\040] )*) |
82 | # \012 |
86 | \012 |
83 | # /gxc; |
87 | /gxc; |
84 | # |
88 | |
85 | # $req =~ /\G$/ |
89 | $req =~ /\G$/ |
86 | # or return $self->respond ("400 bad request"); |
90 | or return $self->respond ("400 bad request"); |
87 | # |
91 | |
88 | # # remove the "," prefix we added to all headers above |
92 | # remove the "," prefix we added to all headers above |
89 | # substr $_, 0, 1, "" |
93 | substr $_, 0, 1, "" |
90 | # for values %hdr; |
94 | for values %hdr; |
|
|
95 | } |
91 | |
96 | |
92 | if ($http == 1.0) { |
97 | if ($http == 1.0) { |
93 | if ($req =~ /^connection\s*:\s*keep-alive/mi) { |
98 | if ($req =~ /^connection\s*:\s*keep-alive/mi) { |
94 | $self->{ohdr} = "connection: keep-alive\015\012"; |
99 | $self->{ohdr} = "connection: keep-alive\015\012"; |
95 | } else { |
100 | } else { |
… | |
… | |
112 | $self->send ("HTTP/1.1 100 go on\015\012") |
117 | $self->send ("HTTP/1.1 100 go on\015\012") |
113 | if $req =~ /^expect:.*\b100-continue\b/i; |
118 | if $req =~ /^expect:.*\b100-continue\b/i; |
114 | |
119 | |
115 | if ($uri =~ m%^/([0-9a-f]+)(M?)$%) { # faces |
120 | if ($uri =~ m%^/([0-9a-f]+)(M?)$%) { # faces |
116 | my $want_meta = $2; |
121 | my $want_meta = $2; |
117 | my $idx = $cf::FACEHASH{pack "H*", $1}; |
122 | my $idx = $cf::face::HASH{pack "H*", $1}; |
118 | |
123 | |
119 | $idx |
124 | $idx |
120 | or do { $self->respond ("404 illegal face name"), next }; |
125 | or do { $self->respond ("404 illegal face name"), next }; |
121 | |
126 | |
122 | if ($req =~ /if-none-match/i) { # dirtiest hack evar |
127 | if ($req =~ /if-none-match/i) { # dirtiest hack evar |
123 | $self->respond ("304 not modified", "", $cache_headers); |
128 | $self->respond ("304 not modified", "", $cache_headers); |
124 | next; |
129 | next; |
125 | } |
130 | } |
126 | |
131 | |
127 | my $type = cf::face::get_type $idx, 1; |
132 | my $type = cf::face::get_type $idx; |
128 | my $data = cf::face::get_data $idx, 1; |
133 | my $data = cf::face::get_data $idx; |
129 | |
134 | |
130 | (my $meta, $data) = unpack "(w/a*)*", $data |
135 | (my $meta, $data) = unpack "(w/a*)*", $data |
131 | if $type & 1; |
136 | if $type & 1; |
132 | |
137 | |
133 | if ($want_meta) { |
138 | if ($want_meta) { |
134 | if ($type & 1) { |
139 | if ($type & 1) { |
135 | $self->respond ("200 OK", $meta, "content-type: text/plain\015\012" . $cache_headers); |
140 | $self->respond ("200 OK", $meta, "content-type: text/plain; charset=utf-8\015\012" . $cache_headers); |
136 | } else { |
141 | } else { |
137 | $self->respond ("404 type $type has no metadata"); |
142 | $self->respond ("404 type $type has no metadata"); |
138 | } |
143 | } |
139 | } else { |
144 | } else { |
140 | $self->respond ("200 OK", $data, (content_type $data) . $cache_headers); |
145 | $self->respond ("200 OK", $data, (content_type $uri, $data) . $cache_headers); |
141 | } |
146 | } |
142 | |
147 | |
143 | } elsif (my $idx = (cf::face::find "res/http$uri") || (cf::face::find "res/http${uri}index.html")) { |
148 | } elsif (my $idx = (cf::face::find "res/http$uri") || (cf::face::find "res/http${uri}index.html")) { |
144 | # TODO: use etag (shudder) |
149 | # TODO: use etag (shudder) |
145 | my $data = cf::face::get_data $idx, 1; |
150 | my $data = cf::face::get_data $idx; |
146 | $self->respond ("200 OK", $data, (content_type $data) . $cache_headers); |
151 | $self->respond ("200 OK", $data, (content_type $uri, $data) . $cache_headers); |
147 | |
152 | |
148 | } elsif (cf::face::find "res/http$uri/index.html") { |
153 | } elsif (cf::face::find "res/http$uri/index.html") { |
149 | $self->respond ("302 dirslash", "", "location: $uri/\015\012"); |
154 | $self->respond ("302 dirslash", "", "location: $uri/\015\012"); |
150 | |
155 | |
151 | } elsif ($uri eq "/debug") { # for debugging |
156 | } elsif ($uri eq "/debug") { # for debugging |
152 | my @body = "<html><body>"; |
157 | my @body = <<EOF; |
153 | |
158 | <html><head><style> |
|
|
159 | th:nth-child(1) { text-align: right; } |
|
|
160 | th:nth-child(2) { text-align: left; } |
|
|
161 | th:nth-child(3) { text-align: right; } |
|
|
162 | th:nth-child(4) { text-align: left; } |
|
|
163 | th:nth-child(5) { text-align: left; } |
|
|
164 | td:nth-child(1) { text-align: right; font-family: monospace;} |
|
|
165 | td:nth-child(2) { text-align: left; font-family: monospace;} |
|
|
166 | td:nth-child(3) { text-align: right; font-family: monospace;} |
|
|
167 | td:nth-child(4) { text-align: left; } |
|
|
168 | td:nth-child(5) { text-align: left; } |
|
|
169 | </style><body> |
|
|
170 | EOF |
154 | for my $type (6, 5, 4, 3, 2, 1, 0) { |
171 | for my $type (6, 5, 4, 3, 2, 1, 0) { |
155 | push @body, "<h1>$type</h1>"; |
172 | push @body, "<h1>$type</h1><table><tr><th>#</th><th>csum</th><th>size</th><th>name</th><th>meta</th></tr>"; |
156 | |
173 | |
157 | for (1 .. cf::face::faces_size - 1) { |
174 | for (1 .. cf::face::faces_size - 1) { |
158 | cf::cede_to_tick; |
175 | cf::cede_to_tick; |
159 | |
176 | |
160 | next if $type != cf::face::get_type $_; |
177 | next if $type != cf::face::get_type $_; |
161 | my $name = cf::face::get_name $_; |
178 | my $name = cf::face::get_name $_; |
162 | my $id = unpack "H*", cf::face::get_chksum $_, 1; |
179 | my $id = unpack "H*", cf::face::get_csum $_, 0; |
163 | push @body, "$_ <a href='$id'>$name ($id)</a>"; |
180 | push @body, "<tr><td>$_</td><td>$id</td><td>$cf::face::SIZE[0][$_]</td><td><a href='$id'>$name</a></td>"; |
164 | push @body, " <a href='${id}M'>(meta)</a>" if $type & 1; |
181 | push @body, "<td><a href='${id}M'>meta</a>" if $type & 1; |
165 | push @body, "<br>"; |
182 | push @body, "</tr>"; |
166 | } |
183 | } |
|
|
184 | |
|
|
185 | push @body, "</table>"; |
167 | } |
186 | } |
168 | |
187 | |
169 | push @body, "</body></html>"; |
188 | push @body, "</body></html>"; |
170 | |
189 | |
171 | my $body = join "", @body; |
190 | my $body = join "", @body; |
172 | utf8::encode $body; |
191 | utf8::encode $body; |
173 | $self->respond ("200 OK", $body, "content-type: text/html\015\012"); |
192 | $self->respond ("200 OK", $body, "content-type: text/html; charset=utf-8\015\012"); |
174 | |
193 | |
175 | } else { |
194 | } else { |
176 | $self->respond ("404 not found"); |
195 | $self->respond ("404 not found"); |
177 | } |
196 | } |
178 | |
197 | |