--- deliantra/server/ext/tcp_http.ext 2012/11/08 03:37:42 1.7 +++ deliantra/server/ext/tcp_http.ext 2017/01/11 15:13:45 1.17 @@ -1,25 +1,34 @@ #! perl # optional depends=tcp -# http server +# http server - this tries to speak enough of http 1.1 (and 1.0) +# to work with browsers. it does not even attempt to be a complete +# implementation, although it should be mostly correct for that it does. sub send { my $self = $_[0]; - $self->{wbuf} .= $_[1]; + if (length $self->{wbuf}) { + $self->{wbuf} .= $_[1]; + } else { + $self->{wbuf} .= $_[1]; - $self->{ww} ||= AE::io $self->{fh}, 1, sub { my $len = syswrite $self->{fh}, $self->{wbuf}; substr $self->{wbuf}, 0, $len, ""; - delete $self->{ww} unless length $self->{wbuf}; - }; + $self->{ww} = AE::io $self->{fh}, 1, sub { + my $len = syswrite $self->{fh}, $self->{wbuf}; + substr $self->{wbuf}, 0, $len, ""; + + delete $self->{ww} unless $len; # in case of errors, stop + delete $self->{ww} unless length $self->{wbuf}; + } if length $self->{wbuf}; + } } sub fatal { my ($self) = @_; $self->send ("HTTP/1.1 500 internal error\015\012"); - delete $self->{rw}; } sub respond { @@ -27,112 +36,188 @@ . "content-length: " . (0 + length $_[2]) . "\015\012" . "access-control-allow-origin: *\015\012" . $_[0]{ohdr} - . "$_[3]\015\012$_[2]"); + . "$_[3]\015\012" . ($_[0]{give_head} ? "" : $_[2])); } -my $cache_headers = "cache-control: max-age=8640000\015\012" +my $cache_headers = "cache-control: public, max-age=8640000\015\012" . "etag: \"0\"\015\012"; -sub content_type { - return "content-type: image/png\015\012" if $_[0] =~ /^\x89PNG/; - return "content-type: image/jpeg\015\012" if $_[0] =~ /^......JFIF/s; - return "content-type: audio/wav\015\012" if $_[0] =~ /^RIFF/; - return "content-type: audio/ogg\015\012" if $_[0] =~ /^OggS/; - return "content-type: text/html\015\012" if $_[0] =~ /^{rbuf} =~ s/^( (?: [^\015]+ | . )+? )\015\012\015\012//xs) { - my $req = $1; + my ($method, $uri, $http, $req, $close, $len); - # we ignore headers atm. + while () { + while ($self->{rbuf} =~ s/^( (?: [^\015]+ | . )+? \015\012)\015\012//xs) { + $req = $1; - $req =~ m%^GET (\S+) HTTP/([0-9.]+)\015\012%i - or return $self->fatal; + # we ignore headers atm. - my $uri = $1; - my $http = $2; + $req =~ m%^(\S+) (\S+) HTTP/([0-9.]+)\015\012%ig + or return $self->respond ("400 bad request"); - if ($http == 1.0) { - if ($req =~ /^connection\s*:\s*keep-alive/mi) { - $self->{ohdr} = "connection: keep-alive\015\012"; - } else { - $self->{ohdr} = "connection: close\015\012"; - delete $self->{rw}; - } - } + $method = uc $1; + $uri = $2; + $http = $3; + + $uri =~ s%^http://[^/]*%%i; # just in case + $uri =~ s/%([0-9a-fA-F][0-9a-fA-F])/chr hex $1/ge; # %-decode + + if (0) { + my %hdr; - $uri =~ s%^http://[^/]*%%i; # just in case + $hdr{lc $1} .= ",$2" + while $req =~ /\G + ([^:\000-\037]*): + [\011\040]* + ((?: [^\012]+ | \012[\011\040] )*) + \012 + /gxc; - cf::debug "HTTP GET: $self->{id} $uri"; + $req =~ /\G$/ + or return $self->respond ("400 bad request"); - if ($uri =~ m%^/([0-9a-f]+)(M?)$%) { # faces - my $want_meta = $2; - my $idx = $cf::FACEHASH{pack "H*", $1}; + # remove the "," prefix we added to all headers above + substr $_, 0, 1, "" + for values %hdr; + } + + if ($http == 1.0) { + if ($req =~ /^connection\s*:\s*keep-alive/mi) { + $self->{ohdr} = "connection: keep-alive\015\012"; + } else { + $self->{ohdr} = "connection: close\015\012"; + $close = 1; + } + } - $idx - or do { $self->respond ("404 illegal face name"), next }; + $self->{give_head} = $method eq "HEAD" + and $method = "GET"; - if ($req =~ /if-none-match/i) { # dirtiest hack evar - $self->respond ("304 not modified", "", $cache_headers); + cf::debug "HTTP $method: $self->{id} $uri"; + + if ($method ne "GET") { + $self->respond ("405 no $method"); next; } - my $type = cf::face::get_type $idx, 1; - my $data = cf::face::get_data $idx, 1; + # not needed for GET, but who knows + $self->send ("HTTP/1.1 100 go on\015\012") + if $req =~ /^expect:.*\b100-continue\b/i; + + if ($uri =~ m%^/([0-9a-f]+)(M?)$%) { # faces + my $want_meta = $2; + my $idx = $cf::face::HASH{pack "H*", $1}; + + $idx + or do { $self->respond ("404 illegal face name"), next }; + + if ($req =~ /if-none-match/i) { # dirtiest hack evar + $self->respond ("304 not modified", "", $cache_headers); + next; + } + + my $type = cf::face::get_type $idx; + my $data = cf::face::get_data $idx; - (my $meta, $data) = unpack "(w/a*)*", $data - if $type & 1; + (my $meta, $data) = unpack "(w/a*)*", $data + if $type & 1; - if ($want_meta) { - if ($type & 1) { - $self->respond ("200 OK", $meta, "content-type: text/plain\015\012" . $cache_headers); + if ($want_meta) { + if ($type & 1) { + $self->respond ("200 OK", $meta, "content-type: text/plain; charset=utf-8\015\012" . $cache_headers); + } else { + $self->respond ("404 type $type has no metadata"); + } } else { - $self->respond ("404 type $type has no metadata"); + $self->respond ("200 OK", $data, (content_type $uri, $data) . $cache_headers); } - } else { - $self->respond ("200 OK", $data, (content_type $data) . $cache_headers); - } - } elsif (my $idx = (cf::face::find "res/http$uri") || (cf::face::find "res/http${uri}index.html")) { - my $data = cf::face::get_data $idx, 1; - $self->respond ("200 OK", $data, (content_type $data) . $cache_headers); - - } elsif (cf::face::find "res/http$uri/index.html") { - $self->respond ("302 dirslash", "", "location: $uri/\015\012"); - - } elsif ($uri eq "/debug") { # for debugging - my @body = ""; - - for my $type (6, 5, 4, 3, 2, 1, 0) { - push @body, "

$type

"; - - for (1 .. cf::face::faces_size - 1) { - next if $type != cf::face::get_type $_; - my $name = cf::face::get_name $_; - my $id = unpack "H*", cf::face::get_chksum $_, 1; - push @body, "$_ $name ($id)"; - push @body, " (meta)" if $type & 1; - push @body, "
"; + } elsif (my $idx = (cf::face::find "res/http$uri") || (cf::face::find "res/http${uri}index.html")) { + my $data = cf::face::get_data $idx; + my $csum = unpack "H*", cf::face::get_csum $idx; + my $hdr = "etag: \"$csum\"\015\012" . $revalidate_headers; + + if ($req =~ /if-none-match: "$csum"/i) { # dirtiest hack evar + $self->respond ("304 not modified", "", $hdr); + next; } - } - push @body, ""; + $self->respond ("200 OK", $data, (content_type $uri, $data) . $hdr); + + } elsif (cf::face::find "res/http$uri/index.html") { + $self->respond ("302 dirslash", "", "location: $uri/\015\012" . $nocache_headers); - $self->respond ("200 OK", (join "", @body), "content-type: text/html\015\012"); - } elsif ($uri eq "/ws" && defined &ext::ws::server) { - &ext::ws::server ($self->{id}, $self->{fh}, "$req\015\012\015\012$self->{rbuf}"); + } elsif ($uri eq "/debug") { # for debugging + my @body = < +EOF + for my $type (6, 5, 4, 3, 2, 1, 0) { + push @body, "

$type

"; + + for (1 .. cf::face::faces_size - 1) { + cf::cede_to_tick; + + next if $type != cf::face::get_type $_; + my $name = cf::face::get_name $_; + my $id = unpack "H*", cf::face::get_csum $_, 0; + push @body, ""; + push @body, ""; + } + + push @body, "
#csumsizenamemeta
$_$id$cf::face::SIZE[0][$_]$namemeta" if $type & 1; + push @body, "
"; + } - %$self = (); + push @body, ""; + + my $body = join "", @body; + utf8::encode $body; + $self->respond ("200 OK", $body, "content-type: text/html; charset=utf-8\015\012"); + + } else { + $self->respond ("404 not found"); + } - } else { - $self->respond ("404 not found"); + cf::cede_to_tick; } - } + + return if $close; # http 1.0 only currently + return if length $self->{rbuf} > 8192; # headers too long + + Coro::AnyEvent::readable $self->{fh}, 6; + + $len = sysread $self->{fh}, $self->{rbuf}, 4096, length $self->{rbuf}; + + return if $len <= 0; + }; } our $DETECTOR = ext::tcp::register http => 64, sub { @@ -146,19 +231,12 @@ wbuf => "", }; - $self->{rw} = AE::io $self->{fh}, 0, sub { - my $len = sysread $self->{fh}, $self->{rbuf}, 4096, length $self->{rbuf}; - - if ($len == 0) { - delete $self->{rw}; - } else { - $self->handle_req; + $self->{async} = Coro::async_pool { + $Coro::current->nice (4); + $Coro::current->{desc} = "http $self->{id}"; - delete $self->{rw} if length $self->{rbuf} > 8192; # headers too long - } + $self->handle_con; }; - - $self->handle_req; # in the unlikely case of the buffer already forming a valid request }; cf::register_exticmd http_faceurl => sub {