ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/tcp_http.ext
Revision: 1.8
Committed: Fri Nov 9 00:11:49 2012 UTC (11 years, 6 months ago) by root
Branch: MAIN
Changes since 1.7: +1 -0 lines
Log Message:
*** empty log message ***

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 # TODO: temp redirect to face itself, for caching, or use etag (shudder)
103 my $data = cf::face::get_data $idx, 1;
104 $self->respond ("200 OK", $data, (content_type $data) . $cache_headers);
105
106 } elsif (cf::face::find "res/http$uri/index.html") {
107 $self->respond ("302 dirslash", "", "location: $uri/\015\012");
108
109 } elsif ($uri eq "/debug") { # for debugging
110 my @body = "<html><body>";
111
112 for my $type (6, 5, 4, 3, 2, 1, 0) {
113 push @body, "<h1>$type</h1>";
114
115 for (1 .. cf::face::faces_size - 1) {
116 next if $type != cf::face::get_type $_;
117 my $name = cf::face::get_name $_;
118 my $id = unpack "H*", cf::face::get_chksum $_, 1;
119 push @body, "$_ <a href='$id'>$name ($id)</a>";
120 push @body, " <a href='${id}M'>(meta)</a>" if $type & 1;
121 push @body, "<br>";
122 }
123 }
124
125 push @body, "</body></html>";
126
127 $self->respond ("200 OK", (join "", @body), "content-type: text/html\015\012");
128 } elsif ($uri eq "/ws" && defined &ext::ws::server) {
129 &ext::ws::server ($self->{id}, $self->{fh}, "$req\015\012\015\012$self->{rbuf}");
130
131 %$self = ();
132
133 } else {
134 $self->respond ("404 not found");
135 }
136 }
137 }
138
139 our $DETECTOR = ext::tcp::register http => 64, sub {
140 # regex avoids conflict with websockets, which use /ws
141 m{^(?i:GET|HEAD|OPTIONS) \ (?! (?i:http://[^/]+)? /ws \ ) }x
142 }, sub {
143 my $self = bless {
144 id => $_[0],
145 fh => $_[1],
146 rbuf => $_[2],
147 wbuf => "",
148 };
149
150 $self->{rw} = AE::io $self->{fh}, 0, sub {
151 my $len = sysread $self->{fh}, $self->{rbuf}, 4096, length $self->{rbuf};
152
153 if ($len == 0) {
154 delete $self->{rw};
155 } else {
156 $self->handle_req;
157
158 delete $self->{rw} if length $self->{rbuf} > 8192; # headers too long
159 }
160 };
161
162 $self->handle_req; # in the unlikely case of the buffer already forming a valid request
163 };
164
165 cf::register_exticmd http_faceurl => sub {
166 my ($ns) = @_;
167
168 "/"
169 };
170