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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines