From 4115c5f2d84d18485e8cc6ab8d04324cb0ebbaa0 Mon Sep 17 00:00:00 2001 From: Hector Castro Date: Fri, 13 Nov 2015 15:55:57 -0600 Subject: [PATCH] 15190 - Bad utf8 decode to unapi and fixing blank screen error status Fix bad utf8 decoding, also fix cgi->header(-status) to redirect to one of the posible error pages in /cgi-bin/koha/errors/ To test 1) Copy from LOC (Z39.50) ISBN 8467020113 2) Open URL cgi-bin/koha/unapi?id=koha:biblionumber:4&format=marcxml 3) (enter the new biblionumber for ISBN 8467020113) 4) A marcxml record will display, notice about the bad utf8 decoding characters 5) Search the record in opac and try to save in Zotero (see bad decoding) 6) To reproduce blank screen (error status) play with: cgi-bin/koha/unapi?id=koha:biblionumber:4 cgi-bin/koha/unapi?id=koha:biblionumber:4&format= cgi-bin/koha/unapi?id=koha:biblionumber:4&format=none cgi-bin/koha/unapi?id=koha:biblionumber:&format=marcxml cgi-bin/koha/unapi?&format=oai_dc cgi-bin/koha/unapi? 7) If possible test in NORMARC (UNIMARC will launch an error because bug 15162) -Apply patch -Follow steps 2 to 6 -You will see no errors in utf8 and redirection to one of the error pages in /cgi-bin/koha/errors/ --- opac/unapi | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/opac/unapi b/opac/unapi index 7b21ee0..a072423 100755 --- a/opac/unapi +++ b/opac/unapi @@ -42,9 +42,9 @@ use C4::Context; use C4::Biblio; use XML::LibXML; use XML::LibXSLT; +use utf8; my $cgi = CGI->new(); -binmode(STDOUT, ":encoding(UTF-8)"); #output as utf8 =head1 VARIABLES @@ -95,6 +95,12 @@ my $format_to_stylesheet_map = { }, }; +my %redirect_error = ( + 'error400' => '/cgi-bin/koha/errors/400.pl', + 'error404' => '/cgi-bin/koha/errors/404.pl', + 'error500' => '/cgi-bin/koha/errors/500.pl' + ); + =head2 $format_info This hashref maps from unAPI output formats to the elements @@ -132,13 +138,14 @@ if (not defined $format) { my $marcxml = GetXmlBiblio($biblionumber); unless (defined $marcxml) { # no bib, so 404 - print $cgi->header( -status => '404 record not found'); + print $cgi->redirect( -uri => $redirect_error{error404} ); exit 0; } my $transformer = get_transformer($format, $format_to_stylesheet_map, $format_info); unless (defined $transformer) { - print $cgi->header( -status => '406 invalid format requested' ); + # invalid format requested + print $cgi->redirect( -uri => $redirect_error{error400} ); exit 0; } my $parser = XML::LibXML->new(); @@ -146,21 +153,22 @@ if (not defined $format) { $record_dom = $transformer->transform( $record_dom ); $content = $record_dom->toString(); }; - if ($@) { - print $cgi->header( -status => '500 internal error ' . $@->code() . ": " . $@->message() ); + if ($@) { #$@->code(), $@->message() can't be passed to 500.pl + print $cgi->redirect( -uri => $redirect_error{error500} ); exit 0; } print $cgi->header( -type =>'application/xml' ); + utf8::decode($content); #output as utf8 print $content; } else { # ID is obviously wrong, so 404 - print $cgi->header( -status => '404 record not found'); + print $cgi->redirect( -uri => $redirect_error{error404} ); exit 0; } } else { # supplied a format but no id - caller is doing it wrong - print $cgi->header( -status => '400 bad request - if you specify format, must specify id'); + print $cgi->redirect( -uri => $redirect_error{error404} ); exit 0; } -- 1.7.10.4