Add inline documentation about chunk size and byte safety collab/miker/revert_perl_encode_byte_counting
authorMike Rylander <miker@esilibrary.com>
Fri, 15 Sep 2017 14:45:50 +0000 (10:45 -0400)
committerMike Rylander <miker@esilibrary.com>
Fri, 15 Sep 2017 14:48:59 +0000 (10:48 -0400)
Since we publish only 7-bit ASCII, it's safe to calculate chunk size in bytes
rather than characters.  This provides some inline comments so that future
developers know that without full context or code diving.

Signed-off-by: Mike Rylander <mrylander@gmail.com>
src/libopensrf/osrf_app_session.c
src/libopensrf/osrf_application.c
src/perl/lib/OpenSRF/AppSession.pm

index 28242e7..60cb948 100644 (file)
@@ -1369,6 +1369,12 @@ int osrfAppRequestRespondComplete(
                size_t data_size = raw_size + extra_size;
                size_t chunk_size = OSRF_MSG_CHUNK_SIZE;
 
+               /* Here we calculate the chunk size in bytes, which is safe because we only
+                * publish 7-bit ASCII.  For any 2+ byte UTF-8, we use the JSON \u-escaping
+                * mechanism.  This makes transfers larger, but is simpler to deal with as
+                * we needn't worry about splitting the string in the middle of a multi-byte
+                * character.
+                */
                if (data_size > chunk_size) // calculate an escape-scaled chunk size
                        chunk_size = ((double)raw_size / (double)data_size) * (double)chunk_size;
 
index 4a5f53e..a981c4a 100644 (file)
@@ -739,6 +739,12 @@ static int _osrfAppRespond( osrfMethodContext* ctx, const jsonObject* data, int
             size_t data_size = raw_size + extra_size;
             size_t chunk_size = ctx->method->max_chunk_size;
 
+            /* Here we calculate the chunk size in bytes, which is safe because we only
+             * publish 7-bit ASCII.  For any 2+ byte UTF-8, we use the JSON \u-escaping
+             * mechanism.  This makes transfers larger, but is simpler to deal with as
+             * we needn't worry about splitting the string in the middle of a multi-byte
+             * character.
+             */
             if (data_size > chunk_size) // calculate an escape-scaled chunk size
                 chunk_size = ((double)raw_size / (double)data_size) * (double)chunk_size;
 
index adcd4ab..34f3d1e 100644 (file)
@@ -1075,7 +1075,7 @@ sub respond {
             }
 
             if ($raw_length > $chunk_size) { # send partials ("chunking")
-                for (my $i = 0; $i < length($str); $i += $chunk_size) {
+                for (my $i = 0; $i < $raw_length; $i += $chunk_size) {
                     $response = new OpenSRF::DomainObject::oilsResult::Partial;
                     $response->content( substr($str, $i, $chunk_size) );
                     $self->session->send($type, $response, $self->threadTrace);