ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/tcp_http.ext
Revision: 1.5
Committed: Tue Nov 6 23:55:54 2012 UTC (11 years, 6 months ago) by root
Branch: MAIN
Changes since 1.4: +4 -1 lines
Log Message:
temporary hack

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