… | |
… | |
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 | } |
29 | } |
|
|
30 | |
|
|
31 | my $cache_headers = "cache-control: max-age=8640000\015\012" |
|
|
32 | . "etag: \"0\"\015\012"; |
30 | |
33 | |
31 | sub handle_req { |
34 | sub handle_req { |
32 | my ($self) = @_; |
35 | my ($self) = @_; |
33 | |
36 | |
34 | while ($self->{rbuf} =~ s/^( ( [^\015]+ | . )+? )\015\012\015\012//xs) { |
37 | while ($self->{rbuf} =~ s/^( ( [^\015]+ | . )+? )\015\012\015\012//xs) { |
… | |
… | |
47 | |
50 | |
48 | if ($uri =~ m%^/([0-9a-f]+)$%) { # faces |
51 | if ($uri =~ m%^/([0-9a-f]+)$%) { # faces |
49 | my $idx = $cf::FACEHASH{pack "H*", $1}; |
52 | my $idx = $cf::FACEHASH{pack "H*", $1}; |
50 | |
53 | |
51 | $idx |
54 | $idx |
52 | or return $self->respond ("404 illegal face name"); |
55 | or do { $self->respond ("404 illegal face name"), next }; |
53 | |
56 | |
54 | if ($req =~ /if-none-match/i) { # dirtiest hack evar |
57 | if ($req =~ /if-none-match/i) { # dirtiest hack evar |
55 | $self->respond ("304 not modified"); |
58 | $self->respond ("304 not modified", "", $cache_headers); |
56 | return; |
59 | next; |
57 | } |
60 | } |
58 | |
61 | |
59 | my $type = cf::face::get_type $idx, 1; |
62 | my $type = cf::face::get_type $idx, 1; |
60 | |
63 | |
61 | if ($type & 1) { |
64 | if ($type & 1) { |
62 | $self->respond ("404 type $type not served yet"); |
65 | $self->respond ("404 type $type not served yet"); |
63 | } else { |
66 | } else { |
64 | if ($type == 0) { # faces |
67 | if ($type == 0) { # faces |
65 | $self->respond ("200 OK", (cf::face::get_data $idx, 1), |
68 | $self->respond ("200 OK", (cf::face::get_data $idx, 1), "content-type: image/png\015\012$cache_headers"); |
66 | "content-type: image/png\015\012" |
|
|
67 | . "cache-control: max-age=864000\015\012" |
|
|
68 | . "etag: \"0\"\015\012" |
|
|
69 | ); |
|
|
70 | } else { |
69 | } else { |
71 | $self->respond ("404 type $type not served yet"); |
70 | $self->respond ("404 type $type not served yet"); |
72 | } |
71 | } |
73 | } |
72 | } |
74 | } elsif ($uri eq "/allimgs") { # for debugging |
73 | } elsif ($uri eq "/allimgs") { # for debugging |
75 | my $body = "<html><body>"; |
74 | my $body = "<html><body>"; |
76 | |
75 | |
77 | for (1 .. cf::face::faces_size - 1) { |
76 | for (1 .. cf::face::faces_size - 1) { |
78 | next if cf::face::get_type $_; |
77 | next if cf::face::get_type $_; |
79 | my $name = cf::face::get_chksum $_, 1; |
78 | my $name = cf::face::get_chksum $_, 1; |
80 | $body .= "<img src='" . (unpack "H*", $name) . "'><br>"; |
79 | $body .= "img src='" . (unpack "H*", $name) . "'<br>"; |
81 | } |
80 | } |
82 | |
81 | |
83 | $body .= "</body></html>"; |
82 | $body .= "</body></html>"; |
84 | |
83 | |
85 | $self->respond ("200 OK", $body, "Content-Type: text/html\015\012"); |
84 | $self->respond ("200 OK", $body, "Content-Type: text/html\015\012"); |