… | |
… | |
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 | $self->{rbuf} =~ s/^( ( [^\015]+ | . )+? )\015\012\015\012//xs |
37 | while ($self->{rbuf} =~ s/^( ( [^\015]+ | . )+? )\015\012\015\012//xs) { |
35 | or return; |
38 | my $req = $1; |
36 | |
39 | |
37 | my $req = $1; |
40 | # we ignore headers atm. |
38 | |
41 | |
39 | # we ignore headers atm. |
42 | $req =~ s%^GET (\S+) HTTP/[0-9.]+\015\012%% |
|
|
43 | or return $self->fatal; |
40 | |
44 | |
41 | $req =~ s%^GET (\S+) HTTP/[0-9.]+\015\012%% |
45 | my $uri = $1; |
42 | or return $self->fatal; |
|
|
43 | |
46 | |
44 | my $uri = $1; |
47 | $uri =~ s%^http://[^/]*%%i; # just in case |
45 | |
48 | |
46 | $uri =~ s%^http://[^/]*%%i; # just in case |
49 | cf::debug "HTTP GET: $self->{id} $uri"; |
47 | |
50 | |
48 | cf::debug "HTTP GET: $self->{id} $uri"; |
51 | if ($uri =~ m%^/([0-9a-f]+)$%) { # faces |
|
|
52 | my $idx = $cf::FACEHASH{pack "H*", $1}; |
49 | |
53 | |
50 | if ($uri =~ m%^/([0-9a-f]+)$%) { # faces |
54 | $idx |
51 | my $idx = $cf::FACEHASH{pack "H*", $1}; |
55 | or return $self->respond ("404 illegal face name"); |
52 | |
56 | |
53 | $idx |
57 | if ($req =~ /if-none-match/i) { # dirtiest hack evar |
54 | or return $self->respond ("404 illegal face name"); |
58 | $self->respond ("304 not modified", "", $cache_headers); |
|
|
59 | return; |
|
|
60 | } |
55 | |
61 | |
56 | my $type = cf::face::get_type $idx, 1; |
62 | my $type = cf::face::get_type $idx, 1; |
57 | |
63 | |
58 | if ($type & 1) { |
64 | if ($type & 1) { |
59 | $self->respond ("404 type $type not served yet"); |
65 | $self->respond ("404 type $type not served yet"); |
|
|
66 | } else { |
|
|
67 | if ($type == 0) { # faces |
|
|
68 | $self->respond ("200 OK", (cf::face::get_data $idx, 1), "content-type: image/png\015\012$cache_headers"); |
|
|
69 | } else { |
|
|
70 | $self->respond ("404 type $type not served yet"); |
|
|
71 | } |
|
|
72 | } |
|
|
73 | } elsif ($uri eq "/allimgs") { # for debugging |
|
|
74 | my $body = "<html><body>"; |
|
|
75 | |
|
|
76 | for (1 .. cf::face::faces_size - 1) { |
|
|
77 | next if cf::face::get_type $_; |
|
|
78 | my $name = cf::face::get_chksum $_, 1; |
|
|
79 | $body .= "img src='" . (unpack "H*", $name) . "'<br>"; |
|
|
80 | } |
|
|
81 | |
|
|
82 | $body .= "</body></html>"; |
|
|
83 | |
|
|
84 | $self->respond ("200 OK", $body, "Content-Type: text/html\015\012"); |
60 | } else { |
85 | } else { |
61 | if ($type == 0) { # faces |
86 | $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 | } |
87 | } |
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 | } |
88 | } |
82 | } |
89 | } |
83 | |
90 | |
84 | # dirty hack: called directly from tcp.ext |
91 | # dirty hack: called directly from tcp.ext |
85 | sub server { |
92 | sub server { |