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.1 by root, Tue Nov 6 01:25:48 2012 UTC vs.
Revision 1.16 by root, Wed Jan 11 12:10:25 2017 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines