1 | #! perl # mandatory |
1 | #! perl # mandatory |
2 | |
2 | |
3 | # http server on base port |
3 | # http server on base port |
4 | |
4 | |
5 | use Coro::AnyEvent; |
5 | use Coro::AnyEvent; |
|
|
6 | |
|
|
7 | our @WEBSOCKETS_FORWARDER = qw(slashflash.pl 13327); |
6 | |
8 | |
7 | sub send { |
9 | sub send { |
8 | my $self = $_[0]; |
10 | my $self = $_[0]; |
9 | |
11 | |
10 | $self->{wbuf} .= $_[1]; |
12 | $self->{wbuf} .= $_[1]; |
… | |
… | |
23 | $self->send ("HTTP/1.1 500 internal error\015\012"); |
25 | $self->send ("HTTP/1.1 500 internal error\015\012"); |
24 | delete $self->{rw}; |
26 | delete $self->{rw}; |
25 | } |
27 | } |
26 | |
28 | |
27 | sub respond { |
29 | sub respond { |
28 | $_[0]->send ("HTTP/1.1 $_[1]\015\012Content-Length: " . (0 + length $_[2]) . "\015\012$_[3]\015\012$_[2]"); |
30 | $_[0]->send ("HTTP/1.1 $_[1]\015\012content-length: " . (0 + length $_[2]) . "\015\012$_[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/x-ogg\015\012" if $_[0] =~ /^OggS/; |
|
|
41 | |
|
|
42 | "content-type: text/plain\015\012" |
|
|
43 | } |
|
|
44 | |
|
|
45 | sub copy { |
|
|
46 | my ($a, $b, $buf) = @_; |
|
|
47 | |
|
|
48 | # ultra-lame |
|
|
49 | Coro::async { |
|
|
50 | while () { |
|
|
51 | while (length $buf) { |
|
|
52 | my $len = syswrite $b, $buf |
|
|
53 | or $! == Errno::EINTR |
|
|
54 | or return; |
|
|
55 | |
|
|
56 | substr $buf, 0, $len, ""; |
|
|
57 | |
|
|
58 | Coro::AnyEvent::writable $b; |
|
|
59 | } |
|
|
60 | |
|
|
61 | Coro::AnyEvent::readable $a; |
|
|
62 | sysread $a, $buf, 4096 |
|
|
63 | or return; |
|
|
64 | } |
|
|
65 | }; |
29 | } |
66 | } |
30 | |
67 | |
31 | sub handle_req { |
68 | sub handle_req { |
32 | my ($self) = @_; |
69 | my ($self) = @_; |
33 | |
70 | |
34 | $self->{rbuf} =~ s/^( ( [^\015]+ | . )+? )\015\012\015\012//xs |
71 | while ($self->{rbuf} =~ s/^( (?: [^\015]+ | . )+? )\015\012\015\012//xs) { |
35 | or return; |
72 | my $req = $1; |
36 | |
73 | |
37 | my $req = $1; |
74 | # we ignore headers atm. |
38 | |
75 | |
39 | # we ignore headers atm. |
76 | $req =~ m%^GET (\S+) HTTP/[0-9.]+\015\012% |
|
|
77 | or return $self->fatal; |
40 | |
78 | |
41 | $req =~ s%^GET (\S+) HTTP/[0-9.]+\015\012%% |
79 | my $uri = $1; |
42 | or return $self->fatal; |
|
|
43 | |
80 | |
44 | my $uri = $1; |
81 | $uri =~ s%^http://[^/]*%%i; # just in case |
45 | |
82 | |
46 | $uri =~ s%^http://[^/]*%%i; # just in case |
83 | cf::debug "HTTP GET: $self->{id} $uri"; |
47 | |
84 | |
48 | cf::debug "HTTP GET: $self->{id} $uri"; |
85 | if ($uri =~ m%^/(M?)([0-9a-f]+)$%) { # faces |
|
|
86 | my $want_meta = $1; |
|
|
87 | my $idx = $cf::FACEHASH{pack "H*", $2}; |
49 | |
88 | |
50 | if ($uri =~ m%^/([0-9a-f]+)$%) { # faces |
89 | $idx |
51 | my $idx = $cf::FACEHASH{pack "H*", $1}; |
90 | or do { $self->respond ("404 illegal face name"), next }; |
52 | |
91 | |
53 | $idx |
92 | if ($req =~ /if-none-match/i) { # dirtiest hack evar |
54 | or return $self->respond ("404 illegal face name"); |
93 | $self->respond ("304 not modified", "", $cache_headers); |
|
|
94 | next; |
|
|
95 | } |
55 | |
96 | |
56 | my $type = cf::face::get_type $idx, 1; |
97 | my $type = cf::face::get_type $idx, 1; |
|
|
98 | my $data = cf::face::get_data $idx, 1; |
57 | |
99 | |
|
|
100 | (my $meta, $data) = unpack "(w/a*)*", $data |
|
|
101 | if $type & 1; |
|
|
102 | |
|
|
103 | if ($want_meta) { |
58 | if ($type & 1) { |
104 | if ($type & 1) { |
|
|
105 | $self->respond ("200 OK", $meta, "content-type: text/plain\015\012" . $cache_headers); |
|
|
106 | } else { |
59 | $self->respond ("404 type $type not served yet"); |
107 | $self->respond ("404 type $type has no metadata"); |
|
|
108 | } |
|
|
109 | } else { |
|
|
110 | $self->respond ("200 OK", $data, (content_type $data) . $cache_headers); |
|
|
111 | } |
|
|
112 | |
|
|
113 | } elsif ($uri eq "/debug") { # for debugging |
|
|
114 | my $body = "<html><body>"; |
|
|
115 | |
|
|
116 | for my $type (6, 5, 4, 3, 2, 1, 0) { |
|
|
117 | $body .= "<h1>$type</h1>"; |
|
|
118 | |
|
|
119 | for (1 .. cf::face::faces_size - 1) { |
|
|
120 | next if $type != cf::face::get_type $_; |
|
|
121 | my $name = cf::face::get_name $_; |
|
|
122 | my $id = unpack "H*", cf::face::get_chksum $_, 1; |
|
|
123 | $body .= "$_ <a href='$id'>$name ($id)</a>"; |
|
|
124 | $body .= " <a href='M$id'>(meta)</a>" if $type & 1; |
|
|
125 | $body .= "<br>"; |
|
|
126 | } |
|
|
127 | } |
|
|
128 | |
|
|
129 | $body .= "</body></html>"; |
|
|
130 | |
|
|
131 | $self->respond ("200 OK", $body, "Content-Type: text/html\015\012"); |
|
|
132 | } elsif ($uri eq "/ws") { |
|
|
133 | my $fh = $self->{fh}; |
|
|
134 | my $buf = "$req\015\012\015\012$self->{rbuf}"; |
|
|
135 | |
|
|
136 | %$self = (); |
|
|
137 | |
|
|
138 | &AnyEvent::Socket::tcp_connect (@WEBSOCKETS_FORWARDER, sub { |
|
|
139 | my ($fh2) = shift; |
|
|
140 | |
|
|
141 | Coro::async { |
|
|
142 | if ($fh2) { |
|
|
143 | my $a = copy $fh, $fh2, $buf; |
|
|
144 | my $b = copy $fh2, $fh; |
|
|
145 | |
|
|
146 | $a->join; |
|
|
147 | $b->join; |
|
|
148 | |
|
|
149 | close $fh2; |
|
|
150 | } |
|
|
151 | |
|
|
152 | close $fh; |
|
|
153 | }; |
|
|
154 | }); |
|
|
155 | |
60 | } else { |
156 | } else { |
61 | if ($type == 0) { # faces |
157 | $self->respond ("404 not found"); |
62 | $self->respond ("200 OK", (cf::face::get_data $idx, 1), "Content-Type: image/png\015\012Cache-Control: max-age=864000\015\012"); |
|
|
63 | } else { |
|
|
64 | $self->respond ("404 type $type not served yet"); |
|
|
65 | } |
|
|
66 | } |
158 | } |
67 | } elsif ($uri eq "/allimgs") { # for debugging |
|
|
68 | my $body = "<html><body>"; |
|
|
69 | |
|
|
70 | for (1 .. cf::face::faces_size - 1) { |
|
|
71 | next if cf::face::get_type $_; |
|
|
72 | my $name = cf::face::get_chksum $_, 1; |
|
|
73 | $body .= "<img src='" . (unpack "H*", $name) . "'><br>"; |
|
|
74 | } |
|
|
75 | |
|
|
76 | $body .= "</body></html>"; |
|
|
77 | |
|
|
78 | $self->respond ("200 OK", $body, "Content-Type: text/html\015\012"); |
|
|
79 | } else { |
|
|
80 | $self->respond ("404 not found"); |
|
|
81 | } |
159 | } |
82 | } |
160 | } |
83 | |
161 | |
84 | # dirty hack: called directly from tcp.ext |
162 | # dirty hack: called directly from tcp.ext |
85 | sub server { |
163 | sub server { |