From 7f56db5a6ea0e0a68c323286967e02228d4f264e Mon Sep 17 00:00:00 2001 From: Marcel de Rooy Date: Wed, 29 Jun 2022 12:16:46 +0000 Subject: [PATCH] Bug 12758: Introduce Koha::XSLT::HTTPS This module now fetches a https stylesheet and returns it as a string to Base. (As a workaround waiting for a real solution in the underlying libraries.) The module can be extended to resolve includes but this requires parsing the xslt code. Test plan: [1] Run t/Koha_XSLT_HTTPS.t Signed-off-by: Marcel de Rooy Signed-off-by: David Nind --- Koha/Exceptions/XSLT.pm | 50 +++++++++++++++++++++++++ Koha/XSLT/HTTPS.pm | 82 +++++++++++++++++++++++++++++++++++++++++ t/Koha_XSLT_HTTPS.t | 59 +++++++++++++++++++++++++++++ 3 files changed, 191 insertions(+) create mode 100644 Koha/Exceptions/XSLT.pm create mode 100644 Koha/XSLT/HTTPS.pm create mode 100755 t/Koha_XSLT_HTTPS.t diff --git a/Koha/Exceptions/XSLT.pm b/Koha/Exceptions/XSLT.pm new file mode 100644 index 0000000000..ecd0b1f829 --- /dev/null +++ b/Koha/Exceptions/XSLT.pm @@ -0,0 +1,50 @@ +package Koha::Exceptions::XSLT; + +# Copyright 2022 Rijksmuseum, Koha Development Team +# +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +=head1 NAME + +Koha::Exceptions::XSLT; + +=head1 SYNOPSIS + +Koha::Exceptions::XSLT::MissingFilename->throw; + +=head1 DESCRIPTION + +Defines a few exceptions for Koha::XSLT:: modules + +=cut + +use Modern::Perl; +use Koha::Exception; +use Exception::Class ( + 'Koha::Exceptions::XSLT' => { + isa => 'Koha::Exception', + }, + 'Koha::Exceptions::XSLT::MissingFilename' => { + isa => 'Koha::Exceptions::XSLT', + description => 'File name required', + }, + 'Koha::Exceptions::XSLT::FetchFailed' => { + isa => 'Koha::Exceptions::XSLT', + description => 'Fetching xslt file failed', + }, +); + +1; diff --git a/Koha/XSLT/HTTPS.pm b/Koha/XSLT/HTTPS.pm new file mode 100644 index 0000000000..834afa02a9 --- /dev/null +++ b/Koha/XSLT/HTTPS.pm @@ -0,0 +1,82 @@ +package Koha::XSLT::HTTPS; + +# Copyright 2022 Rijksmuseum +# +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +=head1 NAME + +Koha::XSLT::HTTPS - Helper module to resolve issues with https stylesheets + +=head1 SYNOPSIS + + Koha::XSLT::HTTPS->load( $filename ); + +=head1 DESCRIPTION + +This module collects the contents of https XSLT styleheets where +libxml2/libxslt fail to do so. This should be considered as a +temporary workaround. + +A similar problem comes up with xslt include files. The module could +be extended to resolve these issues too. What holds me back now, is +the fact that we need to parse the whole xslt code. + +=cut + +use Modern::Perl; +use LWP::UserAgent; + +use Koha::Exceptions::XSLT; + +=head1 METHODS + +=head2 load + + Koha::XSLT::HTTPS->load( $filename ); + +=cut + +sub load { + my ( $class, $filename ) = @_; + + Koha::Exceptions::XSLT::MissingFilename->throw if !$filename; + return { location => $filename } if $filename !~ /^https:\/\//; + + my $ua = LWP::UserAgent->new( ssl_opts => { verify_hostname => 0 } ); + my $resp = $ua->get( $filename ); + if( $resp->is_success ) { + my $contents = $resp->decoded_content; + # $contents = $self->_resolve_includes( $contents ); + return { string => $contents }; + } + Koha::Exceptions::XSLT::FetchFailed->throw; +} + +sub _resolve_includes { +# We could parse the code for includes/imports, fetch them and change refs + my ( $self, $code ) = @_; + # TODO Extend it + return $code; +} + +1; + +=head1 AUTHOR + + Marcel de Rooy, Rijksmuseum Amsterdam, The Netherlands + +=cut diff --git a/t/Koha_XSLT_HTTPS.t b/t/Koha_XSLT_HTTPS.t new file mode 100755 index 0000000000..a5d1da4aaa --- /dev/null +++ b/t/Koha_XSLT_HTTPS.t @@ -0,0 +1,59 @@ +#!/usr/bin/perl + +# Copyright 2022 Rijksmuseum +# +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; + +use Test::Exception; +use Test::MockModule; +use Test::MockObject; +use Test::More tests => 1; + +use Koha::XSLT::HTTPS; + +subtest 'load' => sub { + plan tests => 9; + + # Mock the LWP stuff + my $lwp_object = Test::MockObject->new; + my $response = Test::MockObject->new; + $response->mock( 'is_success', sub { return 0 } ); + $lwp_object->mock( 'get', sub { return $response; } ); + my $lwp_mod = Test::MockModule->new( 'LWP::UserAgent' ); + $lwp_mod->mock( 'new', sub { return $lwp_object } ); + + # Trivial bad input + throws_ok { Koha::XSLT::HTTPS->load; } 'Koha::Exceptions::XSLT::MissingFilename', 'No filename'; + throws_ok { Koha::XSLT::HTTPS->load(q{}); } 'Koha::Exceptions::XSLT::MissingFilename', 'Empty filename'; + my $result = Koha::XSLT::HTTPS->load('filename.xsl'); + is( ref($result), 'HASH', 'Should return hash' ); + is( exists $result->{location}, 1, 'Hash key location found' ); + is( $result->{location}, 'filename.xsl', 'Value for location key' ); + + # Mock returns no success + throws_ok { Koha::XSLT::HTTPS->load('https://myfavoritexsltsite.com/test1.xsl'); } + 'Koha::Exceptions::XSLT::FetchFailed', 'Fetch failed'; + + # Mock returns 'code' + $response->mock( 'is_success', sub { return 1 } ); + $response->mock( 'decoded_content', sub { return 'supposed_xslt_code' } ); + $result = Koha::XSLT::HTTPS->load('https://myfavoritexsltsite.com/test2.xsl'); + is( ref($result), 'HASH', 'Should return hash' ); + is( exists $result->{string}, 1, 'Hash key string found' ); + is( $result->{string}, 'supposed_xslt_code', 'Value for string key' ); +} -- 2.30.2