… | |
… | |
23 | $self->send ("HTTP/1.1 500 internal error\015\012"); |
23 | $self->send ("HTTP/1.1 500 internal error\015\012"); |
24 | delete $self->{rw}; |
24 | delete $self->{rw}; |
25 | } |
25 | } |
26 | |
26 | |
27 | sub respond { |
27 | sub respond { |
28 | $_[0]->send ("HTTP/1.1 $_[1]\015\012Content-Length: " . (0 + length $_[2]) . "\015\012$_[3]\015\012$_[2]"); |
28 | $_[0]->send ("HTTP/1.1 $_[1]\015\012content-length: " . (0 + length $_[2]) . "\015\012$_[3]\015\012$_[2]"); |
|
|
29 | } |
|
|
30 | |
|
|
31 | my $cache_headers = "cache-control: max-age=8640000\015\012" |
|
|
32 | . "etag: \"0\"\015\012"; |
|
|
33 | |
|
|
34 | sub content_type { |
|
|
35 | return "content-type: image/png\015\012" if $_[0] =~ /^\x89PNG/; |
|
|
36 | return "content-type: image/jpeg\015\012" if $_[0] =~ /^......JFIF/s; |
|
|
37 | return "content-type: audio/wav\015\012" if $_[0] =~ /^RIFF/; |
|
|
38 | return "content-type: audio/x-ogg\015\012" if $_[0] =~ /^OggS/; |
|
|
39 | |
|
|
40 | "content-type: text/plain\015\012" |
29 | } |
41 | } |
30 | |
42 | |
31 | sub handle_req { |
43 | sub handle_req { |
32 | my ($self) = @_; |
44 | my ($self) = @_; |
33 | |
45 | |
… | |
… | |
43 | |
55 | |
44 | $uri =~ s%^http://[^/]*%%i; # just in case |
56 | $uri =~ s%^http://[^/]*%%i; # just in case |
45 | |
57 | |
46 | cf::debug "HTTP GET: $self->{id} $uri"; |
58 | cf::debug "HTTP GET: $self->{id} $uri"; |
47 | |
59 | |
48 | if ($uri =~ m%^/([0-9a-f]+)$%) { # faces |
60 | if ($uri =~ m%^/(M?)([0-9a-f]+)$%) { # faces |
|
|
61 | my $want_meta = $1; |
49 | my $idx = $cf::FACEHASH{pack "H*", $1}; |
62 | my $idx = $cf::FACEHASH{pack "H*", $2}; |
50 | |
63 | |
51 | $idx |
64 | $idx |
52 | or return $self->respond ("404 illegal face name"); |
65 | or do { $self->respond ("404 illegal face name"), next }; |
53 | |
66 | |
54 | if ($req =~ /if-none-match/i) { # dirtiest hack evar |
67 | if ($req =~ /if-none-match/i) { # dirtiest hack evar |
55 | $self->respond ("304 not modified"); |
68 | $self->respond ("304 not modified", "", $cache_headers); |
56 | return; |
69 | next; |
57 | } |
70 | } |
58 | |
71 | |
59 | my $type = cf::face::get_type $idx, 1; |
72 | my $type = cf::face::get_type $idx, 1; |
|
|
73 | my $data = cf::face::get_data $idx, 1; |
60 | |
74 | |
|
|
75 | (my $meta, $data) = unpack "(w/a*)*", $data |
|
|
76 | if $type & 1; |
|
|
77 | |
|
|
78 | if ($want_meta) { |
61 | if ($type & 1) { |
79 | if ($type & 1) { |
|
|
80 | $self->respond ("200 OK", $meta, "content-type: text/plain\015\012" . $cache_headers); |
|
|
81 | } else { |
62 | $self->respond ("404 type $type not served yet"); |
82 | $self->respond ("404 type $type has no metadata"); |
|
|
83 | } |
63 | } else { |
84 | } else { |
64 | if ($type == 0) { # faces |
85 | $self->respond ("200 OK", $data, (content_type $data) . $cache_headers); |
65 | $self->respond ("200 OK", (cf::face::get_data $idx, 1), |
|
|
66 | "content-type: image/png\015\012" |
|
|
67 | . "cache-control: max-age=864000\015\012" |
|
|
68 | . "etag: \"0\"\015\012" |
|
|
69 | ); |
|
|
70 | } else { |
|
|
71 | $self->respond ("404 type $type not served yet"); |
|
|
72 | } |
|
|
73 | } |
86 | } |
|
|
87 | |
74 | } elsif ($uri eq "/allimgs") { # for debugging |
88 | } elsif ($uri eq "/debug") { # for debugging |
75 | my $body = "<html><body>"; |
89 | my $body = "<html><body>"; |
76 | |
90 | |
|
|
91 | for my $type (6, 5, 4, 3, 2, 1, 0) { |
|
|
92 | $body .= "<h1>$type</h1>"; |
|
|
93 | |
77 | for (1 .. cf::face::faces_size - 1) { |
94 | for (1 .. cf::face::faces_size - 1) { |
78 | next if cf::face::get_type $_; |
95 | next if $type != cf::face::get_type $_; |
79 | my $name = cf::face::get_chksum $_, 1; |
96 | my $name = cf::face::get_name $_; |
80 | $body .= "<img src='" . (unpack "H*", $name) . "'><br>"; |
97 | my $id = unpack "H*", cf::face::get_chksum $_, 1; |
|
|
98 | $body .= "<a href='$id'>$name ($id)</a>"; |
|
|
99 | $body .= " <a href='M$id'>(meta)</a>" if $type & 1; |
|
|
100 | $body .= "<br>"; |
|
|
101 | } |
81 | } |
102 | } |
82 | |
103 | |
83 | $body .= "</body></html>"; |
104 | $body .= "</body></html>"; |
84 | |
105 | |
85 | $self->respond ("200 OK", $body, "Content-Type: text/html\015\012"); |
106 | $self->respond ("200 OK", $body, "Content-Type: text/html\015\012"); |