Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(static): implement Etag rendering for static files #2333

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

dominik-air
Copy link

@dominik-air dominik-air commented Sep 18, 2024

Summary of Changes

  • Renders ETag for static files and includes it in the Response's headers.
  • If If_None_Match header is present in Requests's headers and its ETag matches the file's ETag an HTTP_304 status code is returned.

Related Issues

#2243

Pull Request Checklist

This is just a reminder about the most common mistakes. Please make sure that you tick all appropriate boxes. But please read our contribution guide at least once; it will save you a few review cycles!

If an item doesn't apply to your pull request, check it anyway to make it apparent that there's nothing to do.

  • Applied changes to both WSGI and ASGI code paths and interfaces (where applicable).
  • Added tests for changed code.
  • Prefixed code comments with GitHub nick and an appropriate prefix.
  • Coding style is consistent with the rest of the framework.
  • Updated documentation for changed code.
    • Added docstrings for any new classes, functions, or modules.
    • Updated docstrings for any modifications to existing code.
    • Updated both WSGI and ASGI docs (where applicable).
    • Added references to new classes, functions, or modules to the relevant RST file under docs/.
    • Updated all relevant supporting documentation files under docs/.
    • A copyright notice is included at the top of any new modules (using your own name or the name of your organization).
    • Changed/added classes/methods/functions have appropriate versionadded, versionchanged, or deprecated directives.
  • Changes (and possible deprecations) have towncrier news fragments under docs/_newsfragments/, with the file name format {issue_number}.{fragment_type}.rst. (Run towncrier --draft to ensure it renders correctly.)

If you have any questions to any of the points above, just submit and ask! This checklist is here to help you, not to deter you from contributing!

PR template inspired by the attrs project.

@dominik-air
Copy link
Author

I still have to go through the docs to see if any updates are needed there. As of now, I want to check with you if that's the intended implementation.

Copy link

codecov bot commented Sep 18, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (4d04e9b) to head (84b9a33).

Additional details and impacted files
@@            Coverage Diff            @@
##            master     #2333   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           64        64           
  Lines         7725      7748   +23     
  Branches      1071      1074    +3     
=========================================
+ Hits          7725      7748   +23     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@dominik-air dominik-air marked this pull request as draft September 18, 2024 15:51
@dominik-air dominik-air marked this pull request as ready for review September 29, 2024 20:18
Copy link
Member

@vytas7 vytas7 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I understand, this is work-in-progress, so I didn't review the details, just provided general feedback on how we want to only call fstat() once per file.

falcon.ETag: ETag generated for the file using its modification time and size.
"""
fh = io.open(file_path, 'rb')
content_length = os.fstat(fh.fileno()).st_size
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't want to run os.fstat() multiple times on the same file handle as it is a real syscall.
We only want to call it once per serving, and save the result in a variable.

try:
if self._fallback_filename is None:
raise falcon.HTTPNotFound()
etag = _generate_etag(self._fallback_filename)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above, we are already calling fstat and handling potential errors elsewhere in this file, we can just reuse the same result.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants