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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines