ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/tcp_http.ext
(Generate patch)

Comparing deliantra/server/ext/tcp_http.ext (file contents):
Revision 1.7 by root, Thu Nov 8 03:37:42 2012 UTC vs.
Revision 1.17 by root, Wed Jan 11 15:13:45 2017 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines