ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/tcp_http.ext
Revision: 1.7
Committed: Thu Nov 8 03:37:42 2012 UTC (11 years, 6 months ago) by root
Branch: MAIN
Changes since 1.6: +8 -3 lines
Log Message:
serve res/http as http root

File Contents

# Content
1 #! perl # optional depends=tcp
2
3 # http server
4
5 sub send {
6 my $self = $_[0];
7
8 $self->{wbuf} .= $_[1];
9
10 $self->{ww} ||= AE::io $self->{fh}, 1, sub {
11 my $len = syswrite $self->{fh}, $self->{wbuf};
12 substr $self->{wbuf}, 0, $len, "";
13
14 delete $self->{ww} unless length $self->{wbuf};
15 };
16 }
17
18 sub fatal {
19 my ($self) = @_;
20
21 $self->send ("HTTP/1.1 500 internal error\015\012");
22 delete $self->{rw};
23 }
24
25 sub respond {
26 $_[0]->send ("HTTP/1.1 $_[1]\015\012"
27 . "content-length: " . (0 + length $_[2]) . "\015\012"
28 . "access-control-allow-origin: *\015\012"
29 . $_[0]{ohdr}
30 . "$_[3]\015\012$_[2]");
31 }
32
33 my $cache_headers = "cache-control: max-age=8640000\015\012"
34 . "etag: \"0\"\015\012";
35
36 sub content_type {
37 return "content-type: image/png\015\012" if $_[0] =~ /^\x89PNG/;
38 return "content-type: image/jpeg\015\012" if $_[0] =~ /^......JFIF/s;
39 return "content-type: audio/wav\015\012" if $_[0] =~ /^RIFF/;
40 return "content-type: audio/ogg\015\012" if $_[0] =~ /^OggS/;
41 return "content-type: text/html\015\012" if $_[0] =~ /^</;
42
43 "content-type: text/plain\015\012"
44 }
45
46 sub handle_req {
47 my ($self) = @_;
48
49 while ($self->{rbuf} =~ s/^( (?: [^\015]+ | . )+? )\015\012\015\012//xs) {
50 my $req = $1;
51
52 # we ignore headers atm.
53
54 $req =~ m%^GET (\S+) HTTP/([0-9.]+)\015\012%i
55 or return $self->fatal;
56
57 my $uri = $1;
58 my $http = $2;
59
60 if ($http == 1.0) {
61 if ($req =~ /^connection\s*:\s*keep-alive/mi) {
62 $self->{ohdr} = "connection: keep-alive\015\012";
63 } else {
64 $self->{ohdr} = "connection: close\015\012";
65 delete $self->{rw};
66 }
67 }
68
69 $uri =~ s%^http://[^/]*%%i; # just in case
70
71 cf::debug "HTTP GET: $self->{id} $uri";
72
73 if ($uri =~ m%^/([0-9a-f]+)(M?)$%) { # faces
74 my $want_meta = $2;
75 my $idx = $cf::FACEHASH{pack "H*", $1};
76
77 $idx
78 or do { $self->respond ("404 illegal face name"), next };
79
80 if ($req =~ /if-none-match/i) { # dirtiest hack evar
81 $self->respond ("304 not modified", "", $cache_headers);
82 next;
83 }
84
85 my $type = cf::face::get_type $idx, 1;
86 my $data = cf::face::get_data $idx, 1;
87
88 (my $meta, $data) = unpack "(w/a*)*", $data
89 if $type & 1;
90
91 if ($want_meta) {
92 if ($type & 1) {
93 $self->respond ("200 OK", $meta, "content-type: text/plain\015\012" . $cache_headers);
94 } else {
95 $self->respond ("404 type $type has no metadata");
96 }
97 } else {
98 $self->respond ("200 OK", $data, (content_type $data) . $cache_headers);
99 }
100
101 } elsif (my $idx = (cf::face::find "res/http$uri") || (cf::face::find "res/http${uri}index.html")) {
102 my $data = cf::face::get_data $idx, 1;
103 $self->respond ("200 OK", $data, (content_type $data) . $cache_headers);
104
105 } elsif (cf::face::find "res/http$uri/index.html") {
106 $self->respond ("302 dirslash", "", "location: $uri/\015\012");
107
108 } elsif ($uri eq "/debug") { # for debugging
109 my @body = "<html><body>";
110
111 for my $type (6, 5, 4, 3, 2, 1, 0) {
112 push @body, "<h1>$type</h1>";
113
114 for (1 .. cf::face::faces_size - 1) {
115 next if $type != cf::face::get_type $_;
116 my $name = cf::face::get_name $_;
117 my $id = unpack "H*", cf::face::get_chksum $_, 1;
118 push @body, "$_ <a href='$id'>$name ($id)</a>";
119 push @body, " <a href='${id}M'>(meta)</a>" if $type & 1;
120 push @body, "<br>";
121 }
122 }
123
124 push @body, "</body></html>";
125
126 $self->respond ("200 OK", (join "", @body), "content-type: text/html\015\012");
127 } elsif ($uri eq "/ws" && defined &ext::ws::server) {
128 &ext::ws::server ($self->{id}, $self->{fh}, "$req\015\012\015\012$self->{rbuf}");
129
130 %$self = ();
131
132 } else {
133 $self->respond ("404 not found");
134 }
135 }
136 }
137
138 our $DETECTOR = ext::tcp::register http => 64, sub {
139 # regex avoids conflict with websockets, which use /ws
140 m{^(?i:GET|HEAD|OPTIONS) \ (?! (?i:http://[^/]+)? /ws \ ) }x
141 }, sub {
142 my $self = bless {
143 id => $_[0],
144 fh => $_[1],
145 rbuf => $_[2],
146 wbuf => "",
147 };
148
149 $self->{rw} = AE::io $self->{fh}, 0, sub {
150 my $len = sysread $self->{fh}, $self->{rbuf}, 4096, length $self->{rbuf};
151
152 if ($len == 0) {
153 delete $self->{rw};
154 } else {
155 $self->handle_req;
156
157 delete $self->{rw} if length $self->{rbuf} > 8192; # headers too long
158 }
159 };
160
161 $self->handle_req; # in the unlikely case of the buffer already forming a valid request
162 };
163
164 cf::register_exticmd http_faceurl => sub {
165 my ($ns) = @_;
166
167 "/"
168 };
169