From 0fc03a89050cfd7f36904776eb28935480e47a38 Mon Sep 17 00:00:00 2001 From: Matt Blenkinsop Date: Tue, 30 Jul 2024 12:54:14 +0000 Subject: [PATCH] Bug 37526: Handle redirects in SUSHI requests --- Koha/ERM/EUsage/UsageDataProvider.pm | 32 ++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/Koha/ERM/EUsage/UsageDataProvider.pm b/Koha/ERM/EUsage/UsageDataProvider.pm index 28a64619359..9a972ee1f8c 100644 --- a/Koha/ERM/EUsage/UsageDataProvider.pm +++ b/Koha/ERM/EUsage/UsageDataProvider.pm @@ -174,9 +174,7 @@ sub harvest_sushi { $self->{begin_date} = $args->{begin_date}; $self->{end_date} = $args->{end_date}; my $url = $self->_build_url_query; - my $request = HTTP::Request->new( 'GET' => $url ); - my $ua = LWP::UserAgent->new; - my $response = $ua->simple_request($request); + my $response = _handle_sushi_request($url); if ( $response->code >= 400 ) { my $result = decode_json( $response->decoded_content ); @@ -277,9 +275,7 @@ sub test_connection { $url .= '&requestor_id=' . $self->requestor_id if $self->requestor_id; $url .= '&api_key=' . $self->api_key if $self->api_key; - my $request = HTTP::Request->new( 'GET' => $url ); - my $ua = LWP::UserAgent->new; - my $response = $ua->simple_request($request); + my $response = _handle_sushi_request($url); if ( $response->{_rc} >= 400 ) { my $message = $response->{_msg}; @@ -517,6 +513,30 @@ sub _counter_file_size_too_large { return 0; } +=head3 _handle_sushi_response + +Creates and sends the request based on a provided url +Also handles any redirects + +=cut + +sub _handle_sushi_request { + my ($url) = @_; + + my $request = HTTP::Request->new( 'GET' => $url ); + my $ua = LWP::UserAgent->new; + $ua->agent( 'Koha/' . Koha::version() ); + my $response = $ua->simple_request($request); + + if ( $response->is_redirect ) { + my $redirect_url = $response->header('Location'); + $redirect_url = URI->new_abs( $redirect_url, $url ); + $response = $ua->get($redirect_url); + } + + return $response; +} + =head3 _type =cut -- 2.39.3 (Apple Git-146)