LP#1709710: Make chunk sizing smart about XML quoting
[OpenSRF.git] / src / libopensrf / osrf_app_session.c
index 5633e1b..28242e7 100644 (file)
@@ -1363,13 +1363,20 @@ int osrfAppRequestRespondComplete(
                        OSRF_STATUS_COMPLETE );
 
        if (data) {
-
                char* json = jsonObjectToJSON(data);
-               size_t data_size = strlen(json);
+               size_t raw_size = strlen(json);
+               size_t extra_size = osrfXmlEscapingLength(json);
+               size_t data_size = raw_size + extra_size;
                size_t chunk_size = OSRF_MSG_CHUNK_SIZE;
-               if (chunk_size > 0 && chunk_size < data_size) {
 
-                       osrfSendChunkedResult(ses, requestId, json, data_size, chunk_size);
+               if (data_size > chunk_size) // calculate an escape-scaled chunk size
+                       chunk_size = ((double)raw_size / (double)data_size) * (double)chunk_size;
+
+               if (chunk_size > 0 && chunk_size < raw_size) {
+                       // chunking -- response message exceeds max message size.
+                       // break it up into chunks for partial delivery
+
+                       osrfSendChunkedResult(ses, requestId, json, raw_size, chunk_size);
                        osrfAppSessionSendBatch( ses, &status, 1 );
 
                } else {