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
ETags are supported for static files but also for other returned objects. Note, that this PR adds digest as a dependency, which can be included as a Suggests if needed.
Headers currently implemented are:
response headers: ETag and Last-Modified
request headers: If-None-Match and If-Modified-Since (Not (yet) implemented are If-Match and IF-Unmodified-Since.
Similar implementations can be found for Go fiber or fastapi.
A simple example of usage is found in the code example:
# setup a static directory with ETag cachingstatic_dir= file.path(tempdir(), "static")
if (!dir.exists(static_dir)) dir.create(static_dir)
file_path= file.path(static_dir, "example.txt")
writeLines("Hello World", file_path)
last_modified= file.info(file_path)[["mtime"]]
file_hash=digest::digest(file=file_path, algo="crc32")
file_hash#> [1] "4425b673"############################################################################## setup the Application with the ETag Middlewareapp=Application$new(middleware=list(ETagMiddleware$new()))
app$add_static(path="/", static_dir)
# Request the file returns the file with ETag headersreq=Request$new(path="/example.txt")
# note that it also returns the Last-Modified and ETag headersapp$process_request(req)
#> <RestRserve Response>#> status code: 200 OK#> content-type: text/plain#> <Headers>#> Server: RestRserve/0.4.1001#> Last-Modified: 2022-03-23T14:48:10Z#> ETag: 4425b673# provide matching hash of the file in the If-None-Match header to check Etag# => 304 Not Modified (Can be cached)req=Request$new(path="/example.txt",
headers=list("If-None-Match"=file_hash))
# note status_code 304 Not Modifiedapp$process_request(req)
#> <RestRserve Response>#> status code: 304 Not Modified#> content-type: text/plain#> <Headers>#> Server: RestRserve/0.4.1001# provide a wrong hash, returns the file normallyreq=Request$new(path="/example.txt",
headers=list("If-None-Match"="WRONG HASH"))
app$process_request(req)
#> <RestRserve Response>#> status code: 200 OK#> content-type: text/plain#> <Headers>#> Server: RestRserve/0.4.1001#> Last-Modified: 2022-03-23T14:48:10Z#> ETag: 4425b673# alternatively, you can provide a timestamp in the If-Modified-Since header# => 304 Not Modified (Can be cached)modified_since= format(last_modified+1, "%FT%TZ")
req=Request$new(path="/example.txt",
headers=list("If-Modified-Since"=modified_since))
app$process_request(req)
#> <RestRserve Response>#> status code: 304 Not Modified#> content-type: text/plain#> <Headers>#> Server: RestRserve/0.4.1001# provide both headers: If-None-Match takes precedence# in this case:# - if none match => modified (No cache)# - if modified since => NOT MODIFIED (cached)# => Overall: modified = no cachemodified_since= format(last_modified+1, "%FT%TZ")
req=Request$new(path="/example.txt",
headers=list("If-None-Match"="CLEARLY WRONG",
"If-Modified-Since"=modified_since))
app$process_request(req)
#> <RestRserve Response>#> status code: 200 OK#> content-type: text/plain#> <Headers>#> Server: RestRserve/0.4.1001#> Last-Modified: 2022-03-23T14:48:10Z#> ETag: 4425b673
@DavZim Thanks for PR. It will take some time to review. I will try this week. In the meantime I will submit v 1.0.0 to CRAN aa they asked to fix failing tests before 2022-03-30. We can include this PR to the next 1.1.0 version.
@dselivanov I know you are busy and I highly appreciate your free maintenance of open source projects.
Do you have a rough time horizon for this PR? Also, is there anything I can take off your shoulders here to speed up the process??
Merging #182 (550d788) into dev (96513b9) will decrease coverage by 0.20%.
The diff coverage is 90.62%.
❗ Current head 550d788 differs from pull request most recent head 8db7150. Consider uploading reports for the commit 8db7150 to get more accurate results
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR adds Middleware for ETags to RestRServe.
ETags are supported for static files but also for other returned objects. Note, that this PR adds
digestas a dependency, which can be included as a Suggests if needed.Headers currently implemented are:
ETagandLast-ModifiedIf-None-MatchandIf-Modified-Since(Not (yet) implemented areIf-MatchandIF-Unmodified-Since.Similar implementations can be found for Go fiber or fastapi.
A simple example of usage is found in the code example: