From 378413299c7f7194160714e9874cb4cde92bbebd Mon Sep 17 00:00:00 2001 From: Paul Eggleton Date: Wed, 29 May 2019 16:22:45 +1200 Subject: [PATCH] Fix progressive task output when it contains entity-escaped characters If an update task's output contains a single quote for example (') then this will be escaped to an HTML entity before it gets sent as a response. However, that means that the length of the data in the response will be greater than the length of the original data, resulting in characters getting missed out when we read the next chunk - so we can't use the escaped length to set the next position to read from. The easiest thing to do is have the Django view send us the actual position we're at and then we don't have to try to calculate it on the JS side. Signed-off-by: Paul Eggleton --- layerindex/views.py | 9 ++++++--- templates/layerindex/task.html | 2 +- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/layerindex/views.py b/layerindex/views.py index ddf6741..b504ff3 100644 --- a/layerindex/views.py +++ b/layerindex/views.py @@ -1537,16 +1537,18 @@ def task_log_view(request, task_id): raise Http404 result = AsyncResult(task_id) - start = request.GET.get('start', 0) + start = int(request.GET.get('start', 0)) try: f = open(os.path.join(settings.TASK_LOG_DIR, 'task_%s.log' % task_id), 'rb') except FileNotFoundError: raise Http404 try: - f.seek(int(start)) + f.seek(start) # We need to escape this or else things that look like tags in the output # will be interpreted as such by the browser - data = escape(f.read()) + datastr = f.read() + origlen = len(datastr) + data = escape(datastr) response = HttpResponse(data) if result.ready(): response['Task-Done'] = '1' @@ -1562,6 +1564,7 @@ def task_log_view(request, task_id): response['Task-Done'] = '0' preader = utils.ProgressReader(settings.TASK_LOG_DIR, task_id) response['Task-Progress'] = preader.read() + response['Task-Log-Position'] = start + origlen finally: f.close() return response diff --git a/templates/layerindex/task.html b/templates/layerindex/task.html index 5928ba1..7c735f0 100644 --- a/templates/layerindex/task.html +++ b/templates/layerindex/task.html @@ -80,7 +80,7 @@ if(scrolling) { task_log.animate({ scrollTop: task_log.prop('scrollHeight') }, "slow"); } - posn += data.length + posn = xhr.getResponseHeader('Task-Log-Position'); done = xhr.getResponseHeader('Task-Done') duration = xhr.getResponseHeader('Task-Duration') progress = parseInt(xhr.getResponseHeader('Task-Progress')) || 0;