<commit_before>package SyTest::Federation::Server;

use strict;
use warnings;

use base qw( SyTest::Federation::_Base Net::Async::HTTP::Server );

no if $] >= 5.017011, warnings => 'experimental::smartmatch';
use feature qw( switch );

use Carp;

use Protocol::Matrix qw( encode_base64_unpadded verify_json_signature );
use HTTP::Headers::Util qw( split_header_words );
use JSON qw( encode_json );

sub configure
{
   my $self = shift;
   my %params = @_;

   foreach (qw( client )) {
      $self->{$_} = delete $params{$_} if exists $params{$_};
   }

   return $self->SUPER::configure( %params );
}

sub _fetch_key
{
   my $self = shift;
   return $self->{client}->_fetch_key( @_ );
}

sub make_request
{
   my $self = shift;
   return SyTest::HTTPServer::Request->new( @_ );
}

sub on_request
{
   my $self = shift;
   my ( $req ) = @_;

   ::log_if_fail "Incoming federation request", $req;

   my $path = $req->path;
   unless( $path =~ s{^/_matrix/}{} ) {
      $req->respond( HTTP::Response->new( 404, "Not Found", [ Content_Length => 0 ] ) );
      return;
   }

   $self->adopt_future(
      ( # 'key' requests don't need to be signed
         $path =~ m{^key/}
            ? Future->done
            : $self->_check_authorization( $req )
      )->then( sub {
         $self->_dispatch( $path, $req )
      })->else_with_f( sub {
         my ( $f, undef, $name ) = @_;
         return $f unless $name eq "matrix_auth";

         # Turn 'matrix_auth' failures into HTTP responses
         my ( undef, $message ) = @_;
         my $body = encode_json {
            errcode => "M_UNAUTHORIZED",
            error   => $message,
         };

         Future->done( response => HTTP::Response->new(
            403, undef, [<commit_msg>Don't 'log_if_fail' the entire incoming federation request object as it's huuuuge<commit_after>package SyTest::Federation::Server;

use strict;
use warnings;

use base qw( SyTest::Federation::_Base Net::Async::HTTP::Server );

no if $] >= 5.017011, warnings => 'experimental::smartmatch';
use feature qw( switch );

use Carp;

use Protocol::Matrix qw( encode_base64_unpadded verify_json_signature );
use HTTP::Headers::Util qw( split_header_words );
use JSON qw( encode_json );

sub configure
{
   my $self = shift;
   my %params = @_;

   foreach (qw( client )) {
      $self->{$_} = delete $params{$_} if exists $params{$_};
   }

   return $self->SUPER::configure( %params );
}

sub _fetch_key
{
   my $self = shift;
   return $self->{client}->_fetch_key( @_ );
}

sub make_request
{
   my $self = shift;
   return SyTest::HTTPServer::Request->new( @_ );
}

sub on_request
{
   my $self = shift;
   my ( $req ) = @_;

   my $path = $req->path;
   unless( $path =~ s{^/_matrix/}{} ) {
      $req->respond( HTTP::Response->new( 404, "Not Found", [ Content_Length => 0 ] ) );
      return;
   }

   $self->adopt_future(
      ( # 'key' requests don't need to be signed
         $path =~ m{^key/}
            ? Future->done
            : $self->_check_authorization( $req )
      )->then( sub {
         $self->_dispatch( $path, $req )
      })->else_with_f( sub {
         my ( $f, undef, $name ) = @_;
         return $f unless $name eq "matrix_auth";

         # Turn 'matrix_auth' failures into HTTP responses
         my ( undef, $message ) = @_;
         my $body = encode_json {
            errcode => "M_UNAUTHORIZED",
            error   => $message,
         };

         Future->done( response => HTTP::Response->new(
            403, undef, [
{'fraction_non_alphanumeric': 0.15946178070426567, 'fraction_numerical': 0.009161179501860864, 'mean_word_length': 2.6624737945492662, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2112537', 'n_tokens_mistral': 1220, 'n_tokens_neox': 1167, 'n_words': 295}