You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
can we add the original content length into an 'Original-Content-Length' header in the after_request func (behind an optional flag in the init method if you don't want it to be a default behaviour), something like the below?
If you would be open to it, I can also do a PR for it? But I didn't want to assume it would be accepted and do the PR without a discussion first.
defafter_request(self, response):
app=self.apporcurrent_appvary=response.headers.get('Vary')
ifnotvary:
response.headers['Vary'] ='Accept-Encoding'elif'accept-encoding'notinvary.lower():
response.headers['Vary'] ='{}, Accept-Encoding'.format(vary)
accept_encoding=request.headers.get('Accept-Encoding', '')
chosen_algorithm=self._choose_compress_algorithm(accept_encoding)
if (chosen_algorithmisNoneorresponse.mimetypenotinapp.config["COMPRESS_MIMETYPES"] orresponse.status_code<200orresponse.status_code>=300or
(response.is_streamedandapp.config["COMPRESS_STREAMS"] isFalse)or"Content-Encoding"inresponse.headersor
(response.content_lengthisnotNoneandresponse.content_length<app.config["COMPRESS_MIN_SIZE"])):
returnresponseresponse.direct_passthrough=False# add original content length for clients that want to update a download progress bar with# the progress event of the `XMLHttpRequest` instance# https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/progress_event# https://github.com/axios/axios/issues/1591#issuecomment-431400903# https://stackoverflow.com/a/42345816/9792594 (non-ideal approximation)response.headers["Original-Content-Length"] =response.content_lengthifself.cacheisnotNone:
key=self.cache_key(request)
compressed_content=self.cache.get(key)
ifcompressed_contentisNone:
compressed_content=self.compress(app, response, chosen_algorithm)
self.cache.set(key, compressed_content)
else:
compressed_content=self.compress(app, response, chosen_algorithm)
response.set_data(compressed_content)
response.headers['Content-Encoding'] =chosen_algorithmresponse.headers['Content-Length'] =response.content_length# compressed length# "123456789" => "123456789:gzip" - A strong ETag validator# W/"123456789" => W/"123456789:gzip" - A weak ETag validatoretag=response.headers.get('ETag')
ifetag:
response.headers['ETag'] ='{0}:{1}"'.format(etag[:-1], chosen_algorithm)
returnresponse
The text was updated successfully, but these errors were encountered:
@alexprengere yeah exactly. Sometimes the Brotli compression factor can be huge (which is great) but then any approximation of the compression factor is way off and the progress bar is almost useless. We’re using flask-compress at my start-up and we’re now adding this custom header to improve that. We’re sending huge files around so progress bars are really important.
I realize it’s non standard so that’s why I asked before doing a PR.
If you are open to it, I could put it behind a flag that is by default False/None and also potentially allow the user to chose the naming of this non standard header. So for most users they wouldn’t notice anything or be affected by it.
Of course, understand if you don’t want it in your library. Just thought I’d offer as we can’t be the only people who will face this challenge.
Feature request:
after_request
func (behind an optional flag in the init method if you don't want it to be a default behaviour), something like the below?If you would be open to it, I can also do a PR for it? But I didn't want to assume it would be accepted and do the PR without a discussion first.
The text was updated successfully, but these errors were encountered: