ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/tcp_http.ext
Revision: 1.13
Committed: Sun Nov 11 06:34:27 2012 UTC (11 years, 6 months ago) by root
Branch: MAIN
Changes since 1.12: +1 -0 lines
Log Message:
*** empty log message ***

File Contents

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