ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/tcp_http.ext
Revision: 1.18
Committed: Sat Jan 28 22:38:45 2017 UTC (7 years, 3 months ago) by root
Branch: MAIN
CVS Tags: HEAD
Changes since 1.17: +8 -2 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 #! perl # optional depends=tcp
2    
3 root 1.14 # http server - this tries to speak enough of http 1.1 (and 1.0)
4 root 1.9 # 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 root 1.1
7     sub send {
8     my $self = $_[0];
9    
10 root 1.9 if (length $self->{wbuf}) {
11     $self->{wbuf} .= $_[1];
12     } else {
13     $self->{wbuf} .= $_[1];
14 root 1.1
15     my $len = syswrite $self->{fh}, $self->{wbuf};
16     substr $self->{wbuf}, 0, $len, "";
17    
18 root 1.9 $self->{ww} = AE::io $self->{fh}, 1, sub {
19     my $len = syswrite $self->{fh}, $self->{wbuf};
20     substr $self->{wbuf}, 0, $len, "";
21    
22 root 1.13 delete $self->{ww} unless $len; # in case of errors, stop
23 root 1.11 delete $self->{ww} unless length $self->{wbuf};
24 root 1.9 } if length $self->{wbuf};
25     }
26 root 1.1 }
27    
28     sub fatal {
29     my ($self) = @_;
30    
31     $self->send ("HTTP/1.1 500 internal error\015\012");
32     }
33    
34 root 1.18 sub respond_304 {
35     $_[0]->send ("HTTP/1.1 304 cache\015\012"
36     . $_[0]{ohdr}
37     . "\015\012");
38     }
39    
40 root 1.1 sub respond {
41     $_[0]->send ("HTTP/1.1 $_[1]\015\012"
42     . "content-length: " . (0 + length $_[2]) . "\015\012"
43     . "access-control-allow-origin: *\015\012"
44 root 1.6 . $_[0]{ohdr}
45 root 1.9 . "$_[3]\015\012" . ($_[0]{give_head} ? "" : $_[2]));
46 root 1.1 }
47    
48 root 1.17 my $cache_headers = "cache-control: public, max-age=8640000\015\012"
49 root 1.1 . "etag: \"0\"\015\012";
50    
51 root 1.17 my $revalidate_headers = "cache-control: public, max-age=60, must-revalidate\015\012";
52    
53     my $nocache_headers = "cache-control: no-cache, max-age=0\015\012";
54    
55 root 1.15 sub content_type($$) {
56 root 1.16 return "content-type: image/png\015\012" if $_[1] =~ /^\x89PNG/;
57     return "content-type: image/jpeg\015\012" if $_[1] =~ /^......JFIF/s;
58     return "content-type: audio/wav\015\012" if $_[1] =~ /^RIFF/;
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] =~ /^</;
63 root 1.1
64 root 1.16 "content-type: text/plain; charset=utf-8\015\012"
65 root 1.1 }
66    
67 root 1.9 sub handle_con {
68 root 1.1 my ($self) = @_;
69    
70 root 1.9 my ($method, $uri, $http, $req, $close, $len);
71 root 1.1
72 root 1.9 while () {
73     while ($self->{rbuf} =~ s/^( (?: [^\015]+ | . )+? \015\012)\015\012//xs) {
74     $req = $1;
75    
76     # we ignore headers atm.
77    
78     $req =~ m%^(\S+) (\S+) HTTP/([0-9.]+)\015\012%ig
79     or return $self->respond ("400 bad request");
80    
81     $method = uc $1;
82     $uri = $2;
83     $http = $3;
84    
85     $uri =~ s%^http://[^/]*%%i; # just in case
86     $uri =~ s/%([0-9a-fA-F][0-9a-fA-F])/chr hex $1/ge; # %-decode
87    
88 root 1.14 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 root 1.9
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 root 1.1
116 root 1.9 $self->{give_head} = $method eq "HEAD"
117     and $method = "GET";
118 root 1.1
119 root 1.9 cf::debug "HTTP $method: $self->{id} $uri";
120 root 1.6
121 root 1.9 if ($method ne "GET") {
122     $self->respond ("405 no $method");
123     next;
124 root 1.6 }
125 root 1.1
126 root 1.9 # 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 root 1.12 my $idx = $cf::face::HASH{pack "H*", $1};
133 root 1.9
134     $idx
135     or do { $self->respond ("404 illegal face name"), next };
136    
137     if ($req =~ /if-none-match/i) { # dirtiest hack evar
138 root 1.18 $self->respond_304;
139 root 1.9 next;
140     }
141 root 1.1
142 root 1.12 my $type = cf::face::get_type $idx;
143     my $data = cf::face::get_data $idx;
144 root 1.1
145 root 1.9 (my $meta, $data) = unpack "(w/a*)*", $data
146     if $type & 1;
147 root 1.1
148 root 1.9 if ($want_meta) {
149     if ($type & 1) {
150 root 1.16 $self->respond ("200 OK", $meta, "content-type: text/plain; charset=utf-8\015\012" . $cache_headers);
151 root 1.9 } else {
152     $self->respond ("404 type $type has no metadata");
153     }
154     } else {
155 root 1.15 $self->respond ("200 OK", $data, (content_type $uri, $data) . $cache_headers);
156 root 1.9 }
157 root 1.1
158 root 1.9 } elsif (my $idx = (cf::face::find "res/http$uri") || (cf::face::find "res/http${uri}index.html")) {
159 root 1.17 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 root 1.18 $self->respond_304;
165 root 1.17 next;
166     }
167    
168     $self->respond ("200 OK", $data, (content_type $uri, $data) . $hdr);
169 root 1.9
170     } elsif (cf::face::find "res/http$uri/index.html") {
171 root 1.17 $self->respond ("302 dirslash", "", "location: $uri/\015\012" . $nocache_headers);
172 root 1.1
173 root 1.9 } elsif ($uri eq "/debug") { # for debugging
174 root 1.14 my @body = <<EOF;
175     <html><head><style>
176     th:nth-child(1) { text-align: right; }
177     th:nth-child(2) { text-align: left; }
178     th:nth-child(3) { text-align: right; }
179     th:nth-child(4) { text-align: left; }
180     th:nth-child(5) { text-align: left; }
181     td:nth-child(1) { text-align: right; font-family: monospace;}
182     td:nth-child(2) { text-align: left; font-family: monospace;}
183     td:nth-child(3) { text-align: right; font-family: monospace;}
184     td:nth-child(4) { text-align: left; }
185     td:nth-child(5) { text-align: left; }
186     </style><body>
187     EOF
188 root 1.9 for my $type (6, 5, 4, 3, 2, 1, 0) {
189 root 1.12 push @body, "<h1>$type</h1><table><tr><th>#</th><th>csum</th><th>size</th><th>name</th><th>meta</th></tr>";
190 root 1.1
191 root 1.9 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 root 1.12 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>";
200 root 1.9 }
201 root 1.12
202     push @body, "</table>";
203 root 1.1 }
204 root 1.9
205     push @body, "</body></html>";
206    
207     my $body = join "", @body;
208     utf8::encode $body;
209 root 1.16 $self->respond ("200 OK", $body, "content-type: text/html; charset=utf-8\015\012");
210 root 1.9
211 root 1.1 } else {
212 root 1.9 $self->respond ("404 not found");
213 root 1.1 }
214    
215 root 1.9 cf::cede_to_tick;
216     }
217 root 1.1
218 root 1.9 return if $close; # http 1.0 only currently
219     return if length $self->{rbuf} > 8192; # headers too long
220 root 1.1
221 root 1.9 Coro::AnyEvent::readable $self->{fh}, 6;
222 root 1.1
223 root 1.9 $len = sysread $self->{fh}, $self->{rbuf}, 4096, length $self->{rbuf};
224 root 1.1
225 root 1.9 return if $len <= 0;
226     };
227 root 1.1 }
228    
229     our $DETECTOR = ext::tcp::register http => 64, sub {
230     # regex avoids conflict with websockets, which use /ws
231 root 1.2 m{^(?i:GET|HEAD|OPTIONS) \ (?! (?i:http://[^/]+)? /ws \ ) }x
232 root 1.1 }, sub {
233     my $self = bless {
234     id => $_[0],
235     fh => $_[1],
236     rbuf => $_[2],
237     wbuf => "",
238     };
239    
240 root 1.9 $self->{async} = Coro::async_pool {
241     $Coro::current->nice (4);
242     $Coro::current->{desc} = "http $self->{id}";
243 root 1.1
244 root 1.9 $self->handle_con;
245 root 1.1 };
246     };
247    
248     cf::register_exticmd http_faceurl => sub {
249     my ($ns) = @_;
250    
251     "/"
252     };
253