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 | delete $self->{ww} 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 | |
… | |
… | |
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/; |
49 | return "content-type: text/html\015\012" if $_[0] =~ /^</; |
50 | return "content-type: text/html\015\012" if $_[1] =~ /^</; |
|
|
51 | return "content-type: text/css\015\012" if $_[0] =~ /\.css$/; |
50 | |
52 | |
51 | "content-type: text/plain\015\012" |
53 | "content-type: text/plain\015\012" |
52 | } |
54 | } |
53 | |
55 | |
54 | sub handle_con { |
56 | sub handle_con { |
… | |
… | |
70 | $http = $3; |
72 | $http = $3; |
71 | |
73 | |
72 | $uri =~ s%^http://[^/]*%%i; # just in case |
74 | $uri =~ s%^http://[^/]*%%i; # just in case |
73 | $uri =~ s/%([0-9a-fA-F][0-9a-fA-F])/chr hex $1/ge; # %-decode |
75 | $uri =~ s/%([0-9a-fA-F][0-9a-fA-F])/chr hex $1/ge; # %-decode |
74 | |
76 | |
|
|
77 | if (0) { |
75 | # my %hdr; |
78 | my %hdr; |
76 | # |
79 | |
77 | # $hdr{lc $1} .= ",$2" |
80 | $hdr{lc $1} .= ",$2" |
78 | # while $req =~ /\G |
81 | while $req =~ /\G |
79 | # ([^:\000-\037]*): |
82 | ([^:\000-\037]*): |
80 | # [\011\040]* |
83 | [\011\040]* |
81 | # ((?: [^\012]+ | \012[\011\040] )*) |
84 | ((?: [^\012]+ | \012[\011\040] )*) |
82 | # \012 |
85 | \012 |
83 | # /gxc; |
86 | /gxc; |
84 | # |
87 | |
85 | # $req =~ /\G$/ |
88 | $req =~ /\G$/ |
86 | # or return $self->respond ("400 bad request"); |
89 | or return $self->respond ("400 bad request"); |
87 | # |
90 | |
88 | # # remove the "," prefix we added to all headers above |
91 | # remove the "," prefix we added to all headers above |
89 | # substr $_, 0, 1, "" |
92 | substr $_, 0, 1, "" |
90 | # for values %hdr; |
93 | for values %hdr; |
|
|
94 | } |
91 | |
95 | |
92 | if ($http == 1.0) { |
96 | if ($http == 1.0) { |
93 | if ($req =~ /^connection\s*:\s*keep-alive/mi) { |
97 | if ($req =~ /^connection\s*:\s*keep-alive/mi) { |
94 | $self->{ohdr} = "connection: keep-alive\015\012"; |
98 | $self->{ohdr} = "connection: keep-alive\015\012"; |
95 | } else { |
99 | } else { |
… | |
… | |
112 | $self->send ("HTTP/1.1 100 go on\015\012") |
116 | $self->send ("HTTP/1.1 100 go on\015\012") |
113 | if $req =~ /^expect:.*\b100-continue\b/i; |
117 | if $req =~ /^expect:.*\b100-continue\b/i; |
114 | |
118 | |
115 | if ($uri =~ m%^/([0-9a-f]+)(M?)$%) { # faces |
119 | if ($uri =~ m%^/([0-9a-f]+)(M?)$%) { # faces |
116 | my $want_meta = $2; |
120 | my $want_meta = $2; |
117 | my $idx = $cf::FACEHASH{pack "H*", $1}; |
121 | my $idx = $cf::face::HASH{pack "H*", $1}; |
118 | |
122 | |
119 | $idx |
123 | $idx |
120 | or do { $self->respond ("404 illegal face name"), next }; |
124 | or do { $self->respond ("404 illegal face name"), next }; |
121 | |
125 | |
122 | if ($req =~ /if-none-match/i) { # dirtiest hack evar |
126 | if ($req =~ /if-none-match/i) { # dirtiest hack evar |
123 | $self->respond ("304 not modified", "", $cache_headers); |
127 | $self->respond ("304 not modified", "", $cache_headers); |
124 | next; |
128 | next; |
125 | } |
129 | } |
126 | |
130 | |
127 | my $type = cf::face::get_type $idx, 1; |
131 | my $type = cf::face::get_type $idx; |
128 | my $data = cf::face::get_data $idx, 1; |
132 | my $data = cf::face::get_data $idx; |
129 | |
133 | |
130 | (my $meta, $data) = unpack "(w/a*)*", $data |
134 | (my $meta, $data) = unpack "(w/a*)*", $data |
131 | if $type & 1; |
135 | if $type & 1; |
132 | |
136 | |
133 | if ($want_meta) { |
137 | if ($want_meta) { |
… | |
… | |
135 | $self->respond ("200 OK", $meta, "content-type: text/plain\015\012" . $cache_headers); |
139 | $self->respond ("200 OK", $meta, "content-type: text/plain\015\012" . $cache_headers); |
136 | } else { |
140 | } else { |
137 | $self->respond ("404 type $type has no metadata"); |
141 | $self->respond ("404 type $type has no metadata"); |
138 | } |
142 | } |
139 | } else { |
143 | } else { |
140 | $self->respond ("200 OK", $data, (content_type $data) . $cache_headers); |
144 | $self->respond ("200 OK", $data, (content_type $uri, $data) . $cache_headers); |
141 | } |
145 | } |
142 | |
146 | |
143 | } elsif (my $idx = (cf::face::find "res/http$uri") || (cf::face::find "res/http${uri}index.html")) { |
147 | } elsif (my $idx = (cf::face::find "res/http$uri") || (cf::face::find "res/http${uri}index.html")) { |
144 | # TODO: use etag (shudder) |
148 | # TODO: use etag (shudder) |
145 | my $data = cf::face::get_data $idx, 1; |
149 | my $data = cf::face::get_data $idx; |
146 | $self->respond ("200 OK", $data, (content_type $data) . $cache_headers); |
150 | $self->respond ("200 OK", $data, (content_type $uri, $data) . $cache_headers); |
147 | |
151 | |
148 | } elsif (cf::face::find "res/http$uri/index.html") { |
152 | } elsif (cf::face::find "res/http$uri/index.html") { |
149 | $self->respond ("302 dirslash", "", "location: $uri/\015\012"); |
153 | $self->respond ("302 dirslash", "", "location: $uri/\015\012"); |
150 | |
154 | |
151 | } elsif ($uri eq "/debug") { # for debugging |
155 | } elsif ($uri eq "/debug") { # for debugging |
152 | my @body = "<html><body>"; |
156 | my @body = <<EOF; |
153 | |
157 | <html><head><style> |
|
|
158 | th:nth-child(1) { text-align: right; } |
|
|
159 | th:nth-child(2) { text-align: left; } |
|
|
160 | th:nth-child(3) { text-align: right; } |
|
|
161 | th:nth-child(4) { text-align: left; } |
|
|
162 | th:nth-child(5) { text-align: left; } |
|
|
163 | td:nth-child(1) { text-align: right; font-family: monospace;} |
|
|
164 | td:nth-child(2) { text-align: left; font-family: monospace;} |
|
|
165 | td:nth-child(3) { text-align: right; font-family: monospace;} |
|
|
166 | td:nth-child(4) { text-align: left; } |
|
|
167 | td:nth-child(5) { text-align: left; } |
|
|
168 | </style><body> |
|
|
169 | EOF |
154 | for my $type (6, 5, 4, 3, 2, 1, 0) { |
170 | for my $type (6, 5, 4, 3, 2, 1, 0) { |
155 | push @body, "<h1>$type</h1>"; |
171 | push @body, "<h1>$type</h1><table><tr><th>#</th><th>csum</th><th>size</th><th>name</th><th>meta</th></tr>"; |
156 | |
172 | |
157 | for (1 .. cf::face::faces_size - 1) { |
173 | for (1 .. cf::face::faces_size - 1) { |
158 | cf::cede_to_tick; |
174 | cf::cede_to_tick; |
159 | |
175 | |
160 | next if $type != cf::face::get_type $_; |
176 | next if $type != cf::face::get_type $_; |
161 | my $name = cf::face::get_name $_; |
177 | my $name = cf::face::get_name $_; |
162 | my $id = unpack "H*", cf::face::get_chksum $_, 1; |
178 | my $id = unpack "H*", cf::face::get_csum $_, 0; |
163 | push @body, "$_ <a href='$id'>$name ($id)</a>"; |
179 | 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; |
180 | push @body, "<td><a href='${id}M'>meta</a>" if $type & 1; |
165 | push @body, "<br>"; |
181 | push @body, "</tr>"; |
166 | } |
182 | } |
|
|
183 | |
|
|
184 | push @body, "</table>"; |
167 | } |
185 | } |
168 | |
186 | |
169 | push @body, "</body></html>"; |
187 | push @body, "</body></html>"; |
170 | |
188 | |
171 | my $body = join "", @body; |
189 | my $body = join "", @body; |