ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/tcp_http.ext
Revision: 1.14
Committed: Mon Nov 12 01:06:38 2012 UTC (11 years, 7 months ago) by root
Branch: MAIN
CVS Tags: rel-3_1
Changes since 1.13: +33 -19 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     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 root 1.6 . $_[0]{ohdr}
39 root 1.9 . "$_[3]\015\012" . ($_[0]{give_head} ? "" : $_[2]));
40 root 1.1 }
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 root 1.7 return "content-type: text/html\015\012" if $_[0] =~ /^</;
51 root 1.1
52     "content-type: text/plain\015\012"
53     }
54    
55 root 1.9 sub handle_con {
56 root 1.1 my ($self) = @_;
57    
58 root 1.9 my ($method, $uri, $http, $req, $close, $len);
59 root 1.1
60 root 1.9 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 root 1.14 if (0) {
77     my %hdr;
78    
79     $hdr{lc $1} .= ",$2"
80     while $req =~ /\G
81     ([^:\000-\037]*):
82     [\011\040]*
83     ((?: [^\012]+ | \012[\011\040] )*)
84     \012
85     /gxc;
86    
87     $req =~ /\G$/
88     or return $self->respond ("400 bad request");
89    
90     # remove the "," prefix we added to all headers above
91     substr $_, 0, 1, ""
92     for values %hdr;
93     }
94 root 1.9
95     if ($http == 1.0) {
96     if ($req =~ /^connection\s*:\s*keep-alive/mi) {
97     $self->{ohdr} = "connection: keep-alive\015\012";
98     } else {
99     $self->{ohdr} = "connection: close\015\012";
100     $close = 1;
101     }
102     }
103 root 1.1
104 root 1.9 $self->{give_head} = $method eq "HEAD"
105     and $method = "GET";
106 root 1.1
107 root 1.9 cf::debug "HTTP $method: $self->{id} $uri";
108 root 1.6
109 root 1.9 if ($method ne "GET") {
110     $self->respond ("405 no $method");
111     next;
112 root 1.6 }
113 root 1.1
114 root 1.9 # not needed for GET, but who knows
115     $self->send ("HTTP/1.1 100 go on\015\012")
116     if $req =~ /^expect:.*\b100-continue\b/i;
117    
118     if ($uri =~ m%^/([0-9a-f]+)(M?)$%) { # faces
119     my $want_meta = $2;
120 root 1.12 my $idx = $cf::face::HASH{pack "H*", $1};
121 root 1.9
122     $idx
123     or do { $self->respond ("404 illegal face name"), next };
124    
125     if ($req =~ /if-none-match/i) { # dirtiest hack evar
126     $self->respond ("304 not modified", "", $cache_headers);
127     next;
128     }
129 root 1.1
130 root 1.12 my $type = cf::face::get_type $idx;
131     my $data = cf::face::get_data $idx;
132 root 1.1
133 root 1.9 (my $meta, $data) = unpack "(w/a*)*", $data
134     if $type & 1;
135 root 1.1
136 root 1.9 if ($want_meta) {
137     if ($type & 1) {
138     $self->respond ("200 OK", $meta, "content-type: text/plain\015\012" . $cache_headers);
139     } else {
140     $self->respond ("404 type $type has no metadata");
141     }
142     } else {
143     $self->respond ("200 OK", $data, (content_type $data) . $cache_headers);
144     }
145 root 1.1
146 root 1.9 } elsif (my $idx = (cf::face::find "res/http$uri") || (cf::face::find "res/http${uri}index.html")) {
147     # TODO: use etag (shudder)
148 root 1.12 my $data = cf::face::get_data $idx;
149 root 1.9 $self->respond ("200 OK", $data, (content_type $data) . $cache_headers);
150    
151     } elsif (cf::face::find "res/http$uri/index.html") {
152     $self->respond ("302 dirslash", "", "location: $uri/\015\012");
153 root 1.1
154 root 1.9 } elsif ($uri eq "/debug") { # for debugging
155 root 1.14 my @body = <<EOF;
156     <html><head><style>
157     th:nth-child(1) { text-align: right; }
158     th:nth-child(2) { text-align: left; }
159     th:nth-child(3) { text-align: right; }
160     th:nth-child(4) { text-align: left; }
161     th:nth-child(5) { text-align: left; }
162     td:nth-child(1) { text-align: right; font-family: monospace;}
163     td:nth-child(2) { text-align: left; font-family: monospace;}
164     td:nth-child(3) { text-align: right; font-family: monospace;}
165     td:nth-child(4) { text-align: left; }
166     td:nth-child(5) { text-align: left; }
167     </style><body>
168     EOF
169 root 1.9 for my $type (6, 5, 4, 3, 2, 1, 0) {
170 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>";
171 root 1.1
172 root 1.9 for (1 .. cf::face::faces_size - 1) {
173     cf::cede_to_tick;
174    
175     next if $type != cf::face::get_type $_;
176     my $name = cf::face::get_name $_;
177 root 1.12 my $id = unpack "H*", cf::face::get_csum $_, 0;
178     push @body, "<tr><td>$_</td><td>$id</td><td>$cf::face::SIZE[0][$_]</td><td><a href='$id'>$name</a></td>";
179     push @body, "<td><a href='${id}M'>meta</a>" if $type & 1;
180     push @body, "</tr>";
181 root 1.9 }
182 root 1.12
183     push @body, "</table>";
184 root 1.1 }
185 root 1.9
186     push @body, "</body></html>";
187    
188     my $body = join "", @body;
189     utf8::encode $body;
190     $self->respond ("200 OK", $body, "content-type: text/html\015\012");
191    
192 root 1.1 } else {
193 root 1.9 $self->respond ("404 not found");
194 root 1.1 }
195    
196 root 1.9 cf::cede_to_tick;
197     }
198 root 1.1
199 root 1.9 return if $close; # http 1.0 only currently
200     return if length $self->{rbuf} > 8192; # headers too long
201 root 1.1
202 root 1.9 Coro::AnyEvent::readable $self->{fh}, 6;
203 root 1.1
204 root 1.9 $len = sysread $self->{fh}, $self->{rbuf}, 4096, length $self->{rbuf};
205 root 1.1
206 root 1.9 return if $len <= 0;
207     };
208 root 1.1 }
209    
210     our $DETECTOR = ext::tcp::register http => 64, sub {
211     # regex avoids conflict with websockets, which use /ws
212 root 1.2 m{^(?i:GET|HEAD|OPTIONS) \ (?! (?i:http://[^/]+)? /ws \ ) }x
213 root 1.1 }, sub {
214     my $self = bless {
215     id => $_[0],
216     fh => $_[1],
217     rbuf => $_[2],
218     wbuf => "",
219     };
220    
221 root 1.9 $self->{async} = Coro::async_pool {
222     $Coro::current->nice (4);
223     $Coro::current->{desc} = "http $self->{id}";
224 root 1.1
225 root 1.9 $self->handle_con;
226 root 1.1 };
227     };
228    
229     cf::register_exticmd http_faceurl => sub {
230     my ($ns) = @_;
231    
232     "/"
233     };
234