Bug 37543: (follow-up) Tidy

Tidy the whole thing

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
Signed-off-by: Paul Derscheid <paul.derscheid@lmscloud.de>
Signed-off-by: Katrin Fischer <katrin.fischer@bsz-bw.de>
This commit is contained in:
Nick Clemens 2024-08-09 10:58:23 +00:00 committed by Katrin Fischer
parent 0e9ea3c9f5
commit cf315751cf
Signed by: kfischer
GPG key ID: 0EF6E2C03357A834

View file

@ -22,7 +22,7 @@ use warnings;
use Getopt::Long qw( GetOptions );
my ($help, $config, $daemon);
my ( $help, $config, $daemon );
GetOptions(
'config|c=s' => \$config,
@ -86,44 +86,46 @@ if ($daemon) {
exit;
{
package ImportProxyServer;
use Carp qw( croak );
use IO::Socket::INET qw( SOCK_STREAM );
# use IO::Socket::IP;
use IO::Select;
use POSIX;
use HTTP::Status qw( HTTP_FORBIDDEN HTTP_UNAUTHORIZED );
use strict;
use warnings;
package ImportProxyServer;
use LWP::UserAgent;
use XML::Simple qw( XMLin );
use MARC::Record;
use MARC::File::XML;
use Carp qw( croak );
use IO::Socket::INET qw( SOCK_STREAM );
use constant CLIENT_READ_TIMEOUT => 5;
use constant CLIENT_READ_BUFFER_SIZE => 100000;
use constant AUTH_URI => "/cgi-bin/koha/svc/authentication";
use constant IMPORT_SVC_URI => "/cgi-bin/koha/svc/import_bib";
# use IO::Socket::IP;
use IO::Select;
use POSIX;
use HTTP::Status qw( HTTP_FORBIDDEN HTTP_UNAUTHORIZED );
use strict;
use warnings;
sub new {
use LWP::UserAgent;
use XML::Simple qw( XMLin );
use MARC::Record;
use MARC::File::XML;
use constant CLIENT_READ_TIMEOUT => 5;
use constant CLIENT_READ_BUFFER_SIZE => 100000;
use constant AUTH_URI => "/cgi-bin/koha/svc/authentication";
use constant IMPORT_SVC_URI => "/cgi-bin/koha/svc/import_bib";
sub new {
my $class = shift;
my $config_file = shift or croak "No config file";
my $self = {time_to_die => 0, config_file => $config_file };
my $self = { time_to_die => 0, config_file => $config_file };
bless $self, $class;
$self->parse_config;
return $self;
}
}
sub parse_config {
sub parse_config {
my $self = shift;
my $config_file = $self->{config_file};
open (my $conf_fh, '<', $config_file) or die "Cannot open config file $config: $!";
open( my $conf_fh, '<', $config_file ) or die "Cannot open config file $config: $!";
my %param;
my $line = 0;
@ -135,7 +137,7 @@ sub parse_config {
s/\s+$//o; # trim trailing spaces
next unless $_;
my ($p, $v) = m/(\S+?):\s*(.*)/o;
my ( $p, $v ) = m/(\S+?):\s*(.*)/o;
die "Invalid config line $line: $_" unless defined $v;
$param{$p} = $v;
}
@ -148,7 +150,8 @@ sub parse_config {
$self->{password} = delete( $param{password} )
or die "No koha user password in config file";
if( defined $param{connexion_user} || defined $param{connexion_password}){
if ( defined $param{connexion_user} || defined $param{connexion_password} ) {
# If either is defined we expect both
$self->{connexion_user} = delete( $param{connexion_user} )
or die "No koha connexion_user in config file";
@ -164,26 +167,26 @@ sub parse_config {
my $log_fh;
close $self->{log_fh} if $self->{log_fh};
if (my $logfile = delete $param{log}) {
open ($log_fh, '>>', $logfile) or die "Cannot open $logfile for write: $!";
if ( my $logfile = delete $param{log} ) {
open( $log_fh, '>>', $logfile ) or die "Cannot open $logfile for write: $!";
} else {
$log_fh = \*STDERR;
}
$self->{log_fh} = $log_fh;
$self->{params} = \%param;
}
}
sub log {
sub log {
my $self = shift;
my $log_fh = $self->{log_fh}
or warn "No log fh",
return;
my $t = localtime;
print $log_fh map "$t: $_\n", @_;
}
}
sub background {
sub background {
my $self = shift;
my $pid = fork;
@ -194,13 +197,14 @@ sub background {
POSIX::setsid() or die "Can't start a new session: $!";
$SIG{INT} = $SIG{TERM} = $SIG{HUP} = sub { $self->{time_to_die} = 1 };
# trap or ignore $SIG{PIPE}
$SIG{USR1} = sub { $self->parse_config };
$self->run;
}
}
sub run {
sub run {
my $self = shift;
my $server_port = $self->{port};
@ -230,23 +234,23 @@ sub run {
}
close($server);
}
}
sub _ua {
sub _ua {
my $ua = LWP::UserAgent->new;
$ua->timeout(10);
$ua->cookie_jar({});
$ua->cookie_jar( {} );
return $ua;
}
}
sub get_current_csrf_token {
sub get_current_csrf_token {
my $self = shift;
my $ua = $self->{ua};
my $url = $self->{koha} . AUTH_URI;
return $ua->get($url)->header('CSRF-TOKEN');
}
}
sub authenticate {
sub authenticate {
my $self = shift;
my $ua = $self->{ua};
my $url = $self->{koha} . AUTH_URI;
@ -259,20 +263,20 @@ sub authenticate {
}
);
if ( !$resp->is_success ) {
$self->log("Authentication failed", $resp->request->as_string, $resp->as_string);
$self->log( "Authentication failed", $resp->request->as_string, $resp->as_string );
return;
}
return $resp->header('CSRF-TOKEN');
}
}
sub read_request {
sub read_request {
my ( $self, $io ) = @_;
my ($in, @in_arr, $timeout, $bad_marc);
my $select = IO::Select->new($io) ;
while ( "FOREVER" ) {
if ( $select->can_read(CLIENT_READ_TIMEOUT) ){
$io->recv($in, CLIENT_READ_BUFFER_SIZE);
my ( $in, @in_arr, $timeout, $bad_marc );
my $select = IO::Select->new($io);
while ("FOREVER") {
if ( $select->can_read(CLIENT_READ_TIMEOUT) ) {
$io->recv( $in, CLIENT_READ_BUFFER_SIZE );
last unless $in;
# XXX ignore after NULL
@ -281,8 +285,7 @@ sub read_request {
last;
}
push @in_arr, $in;
}
else {
} else {
last;
}
}
@ -290,23 +293,23 @@ sub read_request {
$in = join '', @in_arr;
$in =~ m/(.)$/;
my $lastchar = $1;
my ($xml, $user, $password, $local_user);
my ( $xml, $user, $password, $local_user );
my $data = $in; # copy for diagmostic purposes
while () {
my $first = substr( $data, 0, 1 );
if (!defined $first) {
if ( !defined $first ) {
last;
}
$first eq 'U' && do {
($user, $data) = _trim_identifier($data);
( $user, $data ) = _trim_identifier($data);
next;
};
$first eq 'A' && do {
($local_user, $data) = _trim_identifier($data);
( $local_user, $data ) = _trim_identifier($data);
next;
};
$first eq 'P' && do {
($password, $data) = _trim_identifier($data);
( $password, $data ) = _trim_identifier($data);
next;
};
$first eq ' ' && do {
@ -314,13 +317,13 @@ sub read_request {
next;
};
$data =~ m/^[0-9]/ && do {
# What we have here might be a MARC record...
my $marc_record;
eval { $marc_record = MARC::Record->new_from_usmarc($data); };
if ($@) {
$bad_marc = 1;
}
else {
} else {
$xml = $marc_record->as_xml();
}
last;
@ -332,51 +335,51 @@ sub read_request {
push @details, "Timeout" if $timeout;
push @details, "Bad MARC" if $bad_marc;
push @details, "User: $user" if $user;
push @details, "Password: " . ( $self->{debug} ? $password : ("x" x length($password)) ) if $password;
push @details, "Password: " . ( $self->{debug} ? $password : ( "x" x length($password) ) ) if $password;
push @details, "Local user: $local_user" if $local_user;
push @details, "XML: $xml" if $xml;
push @details, "Remaining data: $data" if ($data && !$xml);
push @details, "Remaining data: $data" if ( $data && !$xml );
unless ($xml) {
$self->log("Invalid request", $in, @details);
$self->log( "Invalid request", $in, @details );
return;
}
$user = $local_user if !$user && $local_user;
$self->log("Request", @details);
$self->log( "Request", @details );
$self->log($in) if $self->{debug};
return ($xml, $user, $password);
}
return ( $xml, $user, $password );
}
sub _trim_identifier {
sub _trim_identifier {
#my ($a, $len) = unpack "cc", substr( $_[0], 0, 2 );
my $len=ord(substr ($_[0], 1, 1)) - 64;
if ($len <0) { #length is numeric, and thus comes from the web client, not the desktop client.
my $len = ord( substr( $_[0], 1, 1 ) ) - 64;
if ( $len < 0 ) { #length is numeric, and thus comes from the web client, not the desktop client.
$_[0] =~ m/.(\d+)/;
$len = $1;
return ( substr( $_[0], length($len)+1 , $len ), substr( $_[0], length($len) + 1 + $len ) );
return ( substr( $_[0], length($len) + 1, $len ), substr( $_[0], length($len) + 1 + $len ) );
}
return ( substr( $_[0], 2, $len ), substr( $_[0], 2 + $len ) );
}
return ( substr( $_[0], 2 , $len ), substr( $_[0], 2 + $len ) );
}
sub handle_request {
sub handle_request {
my ( $self, $io ) = @_;
my ($data, $user, $password) = $self->read_request($io)
my ( $data, $user, $password ) = $self->read_request($io)
or return $self->error_response("Bad request");
unless(
!(defined $self->{connexion_user}) ||
($user eq $self->{connexion_user} && $password eq $self->{connexion_password})
){
unless ( !( defined $self->{connexion_user} )
|| ( $user eq $self->{connexion_user} && $password eq $self->{connexion_password} ) )
{
return $self->error_response("Unauthorized request");
}
my $ua;
if ($self->{user}) {
if ( $self->{user} ) {
$user = $self->{user};
$password = $self->{password};
$ua = $self->{ua};
}
else {
} else {
$ua = _ua(); # fresh one, needs to authenticate
}
@ -402,56 +405,57 @@ sub handle_request {
my $status = $resp->code;
if ( $status == HTTP_UNAUTHORIZED || $status == HTTP_FORBIDDEN ) {
# Our token might have expired. Re-authenticate and post again.
$self->{csrf_token} = $self->authenticate;
$resp = $ua->post(
$base_url . IMPORT_SVC_URI,
$post_body,
csrf_token => $self->{csrf_token},
)
);
}
unless ($resp->is_success) {
$self->log("Unsuccessful request", $resp->request->as_string, $resp->as_string);
unless ( $resp->is_success ) {
$self->log( "Unsuccessful request", $resp->request->as_string, $resp->as_string );
return $self->error_response("Unsuccessful request");
}
my ($koha_status, $bib, $overlay, $batch_id, $error, $url);
if ( my $r = eval { XMLin($resp->content) } ) {
my ( $koha_status, $bib, $overlay, $batch_id, $error, $url );
if ( my $r = eval { XMLin( $resp->content ) } ) {
$koha_status = $r->{status};
$batch_id = $r->{import_batch_id};
$error = $r->{error};
$bib = $r->{biblionumber};
$overlay = $r->{match_status};
$url = $r->{url};
}
else {
} else {
$koha_status = "error";
$self->log("Response format error:\n$resp->content");
return $self->error_response("Invalid response");
}
if ($koha_status eq "ok") {
my $response_string = sprintf( "Success. Batch number %s - biblio record number %s",
$batch_id,$bib);
if ( $koha_status eq "ok" ) {
my $response_string = sprintf(
"Success. Batch number %s - biblio record number %s",
$batch_id, $bib
);
$response_string .= $overlay eq 'no_match' ? ' added to Koha.' : ' overlaid by import.';
$response_string .= "\n\n$url";
return $self->response( $response_string );
return $self->response($response_string);
}
return $self->error_response( sprintf( "%s. Please contact administrator.", $error ) );
}
}
sub error_response {
sub error_response {
my $self = shift;
$self->response(@_);
}
}
sub response {
sub response {
my $self = shift;
$self->log("Response: $_[0]");
printf $_[0] . "\0";
}
}
} # package