LP#1612771: Perl max_chunk_size additions
authorBill Erickson <berick@esilibrary.com>
Mon, 24 Feb 2014 20:14:19 +0000 (15:14 -0500)
committerGalen Charlton <gmc@esilibrary.com>
Tue, 1 Nov 2016 21:13:27 +0000 (17:13 -0400)
* Added missing max_chunk_size method to AppSession
* Copy API max_chunk_size value into the handler AppRequest
* Fix error where no-chunking resulted in empty responses

Signed-off-by: Bill Erickson <berick@esilibrary.com>
Signed-off-by: Galen Charlton <gmc@esilibrary.com>
src/perl/lib/OpenSRF/AppSession.pm
src/perl/lib/OpenSRF/Application.pm
src/perl/lib/OpenSRF/DomainObject/oilsResponse.pm

index 7454970..ba3aa54 100644 (file)
@@ -892,6 +892,13 @@ sub max_bundle_size {
        return $self->{max_bundle_size};
 }
 
+sub max_chunk_size {
+       my $self = shift;
+       my $value = shift;
+       $self->{max_chunk_size} = $value if (defined($value));
+       return $self->{max_chunk_size};
+}
+
 sub recv_timeout {
        my $self = shift;
        my $timeout = shift;
@@ -1034,27 +1041,31 @@ sub respond {
        my $msg = shift;
        return unless ($self and $self->session and !$self->complete);
 
-
     my $type = 'RESULT';
        my $response;
        if (ref($msg) && UNIVERSAL::isa($msg, 'OpenSRF::DomainObject::oilsResponse')) {
                $response = $msg;
         $type = 'STATUS' if UNIVERSAL::isa($response, 'OpenSRF::DomainObject::oilsStatus');
-       } elsif ($self->max_chunk_size > 0) { # we might need to chunk
-        my $str = OpenSRF::Utils::JSON->perl2JSON($msg);
-        if (length($str) > $self->max_chunk_size) { # send partials ("chunking")
-            for (my $i = 0; $i < length($str); $i += $self->max_chunk_size) {
-                $response = new OpenSRF::DomainObject::oilsResult::Partial;
-                       $response->content( substr($str, $i, $self->max_chunk_size) );
-                   $self->session->send($type, $response, $self->threadTrace);
+
+       } else {
+
+        if ($self->max_chunk_size > 0) { # we might need to chunk
+            my $str = OpenSRF::Utils::JSON->perl2JSON($msg);
+            if (length($str) > $self->max_chunk_size) { # send partials ("chunking")
+                for (my $i = 0; $i < length($str); $i += $self->max_chunk_size) {
+                    $response = new OpenSRF::DomainObject::oilsResult::Partial;
+                    $response->content( substr($str, $i, $self->max_chunk_size) );
+                    $self->session->send($type, $response, $self->threadTrace);
+                }
+                # This triggers reconstruction on the remote end
+                $response = new OpenSRF::DomainObject::oilsResult::PartialComplete;
+                return $self->session->send($type, $response, $self->threadTrace);
             }
-            # This triggers reconstruction on the remote end
-            $response = new OpenSRF::DomainObject::oilsResult::PartialComplete;
-               return $self->session->send($type, $response, $self->threadTrace);
-        } else {
-            $response = new OpenSRF::DomainObject::oilsResult;
-            $response->content( $msg );
         }
+
+        # message failed to exceed max chunk size OR chunking disabled
+        $response = new OpenSRF::DomainObject::oilsResult;
+        $response->content($msg);
     }
 
     if ($self->{max_bundle_count} > 0 or $self->{max_bundle_size} > 0) { # we are bundling, and we need to test the size or count
index 9749a1d..8aec164 100644 (file)
@@ -65,7 +65,7 @@ sub max_chunk_size {
        my $self = shift;
        return 0 unless ref($self);
        return $self->{max_chunk_size} if (defined($self->{max_chunk_size}));
-       return 2 * $self->max_bundle_size;
+       return 104858; # 1/10 MB
 }
 
 sub api_name {
@@ -182,6 +182,7 @@ sub handler {
                        my $appreq = OpenSRF::AppRequest->new( $session );
                        $appreq->max_bundle_size( $coderef->max_bundle_size );
                        $appreq->max_bundle_count( $coderef->max_bundle_count );
+                       $appreq->max_chunk_size( $coderef->max_chunk_size );
 
                        $log->debug( "in_request = $in_request : [" . $appreq->threadTrace."]", INTERNAL );
                        if( $in_request ) {
index 25d8f50..307eb57 100644 (file)
@@ -13,7 +13,8 @@ BEGIN {
                                        STATUS_NOTFOUND STATUS_NOTALLOWED STATUS_TIMEOUT
                                        STATUS_INTERNALSERVERERROR STATUS_NOTIMPLEMENTED
                                        STATUS_VERSIONNOTSUPPORTED STATUS_REDIRECTED 
-                                       STATUS_EXPFAILED STATUS_COMPLETE/;
+                                       STATUS_EXPFAILED STATUS_COMPLETE STATUS_PARTIAL
+                                       STATUS_NOCONTENT/;
 
 %EXPORT_TAGS = (
        status => [ qw/STATUS_CONTINUE STATUS_OK STATUS_ACCEPTED
@@ -21,7 +22,8 @@ BEGIN {
                                        STATUS_NOTFOUND STATUS_NOTALLOWED STATUS_TIMEOUT
                                        STATUS_INTERNALSERVERERROR STATUS_NOTIMPLEMENTED
                                        STATUS_VERSIONNOTSUPPORTED STATUS_REDIRECTED 
-                                       STATUS_EXPFAILED STATUS_COMPLETE/ ],
+                                       STATUS_EXPFAILED STATUS_COMPLETE STATUS_PARTIAL
+                                       STATUS_NOCONTENT/ ],
 );
 
 }
@@ -284,7 +286,10 @@ package OpenSRF::DomainObject::oilsResult::Partial;
 use OpenSRF::DomainObject::oilsResponse qw/:status/;
 use base 'OpenSRF::DomainObject::oilsResult';
 use vars qw/$status $statusCode/;
-OpenSRF::Utils::JSON->register_class_hint( hint => 'osrfResult', name => 'OpenSRF::DomainObject::oilsResult::Partial', type => 'hash' );
+OpenSRF::Utils::JSON->register_class_hint(
+    hint => 'osrfResultPartial',
+    name => 'OpenSRF::DomainObject::oilsResult::Partial',
+    type => 'hash');
 
 
 $status = 'Partial Response';
@@ -324,7 +329,10 @@ package OpenSRF::DomainObject::oilsResult::PartialComplete;
 use OpenSRF::DomainObject::oilsResponse qw/:status/;
 use base 'OpenSRF::DomainObject::oilsResult';
 use vars qw/$status $statusCode/;
-OpenSRF::Utils::JSON->register_class_hint( hint => 'osrfResult', name => 'OpenSRF::DomainObject::oilsResult::Partial', type => 'hash' );
+OpenSRF::Utils::JSON->register_class_hint( 
+    hint => 'osrfResultPartialComplete',
+    name => 'OpenSRF::DomainObject::oilsResult::PartialComplete',
+    type => 'hash');
 
 
 $status = 'Partial Response Finalized';