1 | #! perl # optional depends=tcp |
1 | #! perl # optional depends=tcp |
2 | |
2 | |
3 | # http server on base port |
3 | # http server |
4 | |
|
|
5 | use Coro::AnyEvent; |
|
|
6 | |
4 | |
7 | sub send { |
5 | sub send { |
8 | my $self = $_[0]; |
6 | my $self = $_[0]; |
9 | |
7 | |
10 | $self->{wbuf} .= $_[1]; |
8 | $self->{wbuf} .= $_[1]; |
… | |
… | |
58 | |
56 | |
59 | $uri =~ s%^http://[^/]*%%i; # just in case |
57 | $uri =~ s%^http://[^/]*%%i; # just in case |
60 | |
58 | |
61 | cf::debug "HTTP GET: $self->{id} $uri"; |
59 | cf::debug "HTTP GET: $self->{id} $uri"; |
62 | |
60 | |
63 | if ($uri =~ m%^/(M?)([0-9a-f]+)$%) { # faces |
61 | if ($uri =~ m%^/([0-9a-f]+)(M?)$%) { # faces |
64 | my $want_meta = $1; |
62 | my $want_meta = $2; |
65 | my $idx = $cf::FACEHASH{pack "H*", $2}; |
63 | my $idx = $cf::FACEHASH{pack "H*", $1}; |
66 | |
64 | |
67 | $idx |
65 | $idx |
68 | or do { $self->respond ("404 illegal face name"), next }; |
66 | or do { $self->respond ("404 illegal face name"), next }; |
69 | |
67 | |
70 | if ($req =~ /if-none-match/i) { # dirtiest hack evar |
68 | if ($req =~ /if-none-match/i) { # dirtiest hack evar |
… | |
… | |
87 | } else { |
85 | } else { |
88 | $self->respond ("200 OK", $data, (content_type $data) . $cache_headers); |
86 | $self->respond ("200 OK", $data, (content_type $data) . $cache_headers); |
89 | } |
87 | } |
90 | |
88 | |
91 | } elsif ($uri eq "/debug") { # for debugging |
89 | } elsif ($uri eq "/debug") { # for debugging |
92 | my $body = "<html><body>"; |
90 | my @body = "<html><body>"; |
93 | |
91 | |
94 | for my $type (6, 5, 4, 3, 2, 1, 0) { |
92 | for my $type (6, 5, 4, 3, 2, 1, 0) { |
95 | $body .= "<h1>$type</h1>"; |
93 | push @body, "<h1>$type</h1>"; |
96 | |
94 | |
97 | for (1 .. cf::face::faces_size - 1) { |
95 | for (1 .. cf::face::faces_size - 1) { |
98 | next if $type != cf::face::get_type $_; |
96 | next if $type != cf::face::get_type $_; |
99 | my $name = cf::face::get_name $_; |
97 | my $name = cf::face::get_name $_; |
100 | my $id = unpack "H*", cf::face::get_chksum $_, 1; |
98 | my $id = unpack "H*", cf::face::get_chksum $_, 1; |
101 | $body .= "$_ <a href='$id'>$name ($id)</a>"; |
99 | push @body, "$_ <a href='$id'>$name ($id)</a>"; |
102 | $body .= " <a href='M$id'>(meta)</a>" if $type & 1; |
100 | push @body, " <a href='${id}M'>(meta)</a>" if $type & 1; |
103 | $body .= "<br>"; |
101 | push @body, "<br>"; |
104 | } |
102 | } |
105 | } |
103 | } |
106 | |
104 | |
107 | $body .= "</body></html>"; |
105 | push @body, "</body></html>"; |
108 | |
106 | |
109 | $self->respond ("200 OK", $body, "Content-Type: text/html\015\012"); |
107 | $self->respond ("200 OK", (join "", @body), "Content-Type: text/html\015\012"); |
110 | } elsif ($uri eq "/ws" && defined &ext::ws::server) { |
108 | } elsif ($uri eq "/ws" && defined &ext::ws::server) { |
111 | &ext::ws::server ($self->{id}, $self->{fh}, "$req\015\012\015\012$self->{rbuf}"); |
109 | &ext::ws::server ($self->{id}, $self->{fh}, "$req\015\012\015\012$self->{rbuf}"); |
112 | |
110 | |
113 | %$self = (); |
111 | %$self = (); |
114 | |
112 | |
… | |
… | |
118 | } |
116 | } |
119 | } |
117 | } |
120 | |
118 | |
121 | our $DETECTOR = ext::tcp::register http => 64, sub { |
119 | our $DETECTOR = ext::tcp::register http => 64, sub { |
122 | # regex avoids conflict with websockets, which use /ws |
120 | # regex avoids conflict with websockets, which use /ws |
123 | m{^(?:(?i)GET|HEAD|OPTIONS) \ (?: [^/] | /[^w] | /w[^s] /ws[^\ ] ) }x |
121 | m{^(?i:GET|HEAD|OPTIONS) \ (?! (?i:http://[^/]+)? /ws \ ) }x |
124 | }, sub { |
122 | }, sub { |
125 | my $self = bless { |
123 | my $self = bless { |
126 | id => $_[0], |
124 | id => $_[0], |
127 | fh => $_[1], |
125 | fh => $_[1], |
128 | rbuf => $_[2], |
126 | rbuf => $_[2], |