From 498003feae7f8cfdcebae10fe5f912453ad04b61 Mon Sep 17 00:00:00 2001 From: aguaviva Date: Fri, 18 Feb 2022 01:31:35 +0100 Subject: [PATCH 1/3] proof of concept, not tested! --- MicroWebSrv2/httpResponse.py | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/MicroWebSrv2/httpResponse.py b/MicroWebSrv2/httpResponse.py index 3d5fbe1..5f0dd34 100644 --- a/MicroWebSrv2/httpResponse.py +++ b/MicroWebSrv2/httpResponse.py @@ -352,16 +352,24 @@ def ReturnFile(self, filename, attachmentName=None) : raise ValueError('"filename" must be a not empty string.') if attachmentName is not None and not isinstance(attachmentName, str) : raise ValueError('"attachmentName" must be a string or None.') + try : - size = stat(filename)[6] - except : - self.ReturnNotFound() - return + size = stat(filename+".gz")[6] + except: + try : + size = stat(filename)[6] + except : + self.ReturnNotFound() + return try : - file = open(filename, 'rb') + file = open(filename+".gz", 'rb') + self.SetHeader('Content-Encoding', "gzip") except : - self.ReturnForbidden() - return + try : + file = open(filename, 'rb') + except : + self.ReturnForbidden() + return if attachmentName : cd = 'attachment; filename="%s"' % attachmentName.replace('"', "'") self.SetHeader('Content-Disposition', cd) From d9549f4cb7fee57c83c94384b9af1a650a6273df Mon Sep 17 00:00:00 2001 From: aguaviva Date: Fri, 18 Feb 2022 01:55:37 +0100 Subject: [PATCH 2/3] this way si better --- MicroWebSrv2/httpResponse.py | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/MicroWebSrv2/httpResponse.py b/MicroWebSrv2/httpResponse.py index 5f0dd34..5489041 100644 --- a/MicroWebSrv2/httpResponse.py +++ b/MicroWebSrv2/httpResponse.py @@ -352,24 +352,20 @@ def ReturnFile(self, filename, attachmentName=None) : raise ValueError('"filename" must be a not empty string.') if attachmentName is not None and not isinstance(attachmentName, str) : raise ValueError('"attachmentName" must be a string or None.') - try : size = stat(filename+".gz")[6] - except: + filename += ".gz" + except : try : size = stat(filename)[6] except : self.ReturnNotFound() return try : - file = open(filename+".gz", 'rb') - self.SetHeader('Content-Encoding', "gzip") + file = open(filename, 'rb') except : - try : - file = open(filename, 'rb') - except : - self.ReturnForbidden() - return + self.ReturnForbidden() + return if attachmentName : cd = 'attachment; filename="%s"' % attachmentName.replace('"', "'") self.SetHeader('Content-Disposition', cd) From b1c40ff50cab1fef4b6fa985d380177ba9724aeb Mon Sep 17 00:00:00 2001 From: aguaviva Date: Fri, 18 Feb 2022 10:53:33 +0100 Subject: [PATCH 3/3] setting now the right header --- MicroWebSrv2/httpResponse.py | 1 + 1 file changed, 1 insertion(+) diff --git a/MicroWebSrv2/httpResponse.py b/MicroWebSrv2/httpResponse.py index 5489041..034a412 100644 --- a/MicroWebSrv2/httpResponse.py +++ b/MicroWebSrv2/httpResponse.py @@ -355,6 +355,7 @@ def ReturnFile(self, filename, attachmentName=None) : try : size = stat(filename+".gz")[6] filename += ".gz" + self.SetHeader('Content-Encoding', 'gzip') except : try : size = stat(filename)[6]