drf-extensions
drf-extensions copied to clipboard
is_if_none_match_failed: check for unquoted etag in list of quoted etags.
Hey folks :wave:
I ran into an issue using a local copy of drf-extensions, and I was just going to suggest a fix when I found #197 had fixed the issue by stripping quotes from etags:
if res_etag and if_none_match:
etags = [etag.strip('"') for etag in etags]
return res_etag in etags or '*' in etags
I was going to suggest this, which is a little bit easier to read IMO:
if res_etag and if_none_match:
return quote_etag(res_etag) in etags or '*' in etags
I presume the fix from #197 should also have been applied to is_if_match_failed too.
I agree with your suggestion to fix is_if_match_failed. I spent hours having "Precondition Failed" on a PUT request. This means both functions require striped etags therefore, they should be done out side both functions, probably before calling both functions or parse_etags function should return striped tags ?
I also see another issue with line:
etags = [etag.strip('"') for etag in etags]
if we have weak etag its not striped properly and matching will fail e.g
if_match='W/"6ff096757676aaecfe35d932aa04e4f8"'
res_etag='6ff096757676aaecfe35d932aa04e4f8'
etags=['W/"6ff096757676aaecfe35d932aa04e4f8"']
etags = [etag.strip('"') for etag in etags]
#estags : ['W/"6ff096757676aaecfe35d932aa04e4f8']
# A better option seems
etags = [etag.strip('W/"') for etag in etags]
# ['6ff096757676aaecfe35d932aa04e4f8']
I am not sure what will be consequences given 1.11 documentation read as:
Changed in Django 1.11:
In older versions, the return value from etag_func() was interpreted as the unquoted part of the ETag. That prevented the use of weak ETags, which have the format W/"
". The return value is now expected to be an ETag as defined by the specification (including the quotes), although the unquoted format is also accepted for backwards compatibility.