-
-
Notifications
You must be signed in to change notification settings - Fork 158
Adds support for ETag header in GET and HEAD requests #998
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d47661f
Refactored IntegrationTest to enable callers to pass custom request h…
d3d2f00
Adds support for HEAD requests
4df5ead
Adds ETag support for read-only requests
b129c9f
Fixed: only send Content-Type header when a response body exists
b0ed62c
Added documentation
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# Caching with ETags | ||
|
||
_since v4.2_ | ||
|
||
GET requests return an [ETag](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag) HTTP header, which can be used by the client in subsequent requests to save network bandwidth. | ||
|
||
Be aware that the returned ETag represents the entire response body (a 'resource' in HTTP terminology) for a request URL that includes the query string. | ||
This is unrelated to JSON:API resources. Therefore, we do not use ETags for optimistic concurrency. | ||
|
||
Getting a list of resources returns an ETag: | ||
|
||
```http | ||
GET /articles?sort=-lastModifiedAt HTTP/1.1 | ||
Host: localhost:5000 | ||
``` | ||
|
||
```http | ||
HTTP/1.1 200 OK | ||
Content-Type: application/vnd.api+json | ||
Server: Kestrel | ||
Transfer-Encoding: chunked | ||
ETag: "7FFF010786E2CE8FC901896E83870E00" | ||
|
||
{ | ||
"data": [ ... ] | ||
} | ||
``` | ||
|
||
The request is later resent using the received ETag. The server data has not changed at this point. | ||
|
||
```http | ||
GET /articles?sort=-lastModifiedAt HTTP/1.1 | ||
Host: localhost:5000 | ||
If-None-Match: "7FFF010786E2CE8FC901896E83870E00" | ||
``` | ||
|
||
```http | ||
HTTP/1.1 304 Not Modified | ||
Server: Kestrel | ||
ETag: "7FFF010786E2CE8FC901896E83870E00" | ||
``` | ||
|
||
After some time, the server data has changed. | ||
|
||
```http | ||
GET /articles?sort=-lastModifiedAt HTTP/1.1 | ||
Host: localhost:5000 | ||
If-None-Match: "7FFF010786E2CE8FC901896E83870E00" | ||
``` | ||
|
||
```http | ||
HTTP/1.1 200 OK | ||
Content-Type: application/vnd.api+json | ||
Server: Kestrel | ||
Transfer-Encoding: chunked | ||
ETag: "356075D903B8FE8D9921201A7E7CD3F9" | ||
|
||
{ | ||
"data": [ ... ] | ||
} | ||
``` | ||
|
||
Note: To just poll for changes (without fetching them), send a HEAD request instead: | ||
|
||
```http | ||
HEAD /articles?sort=-lastModifiedAt HTTP/1.1 | ||
Host: localhost:5000 | ||
If-None-Match: "7FFF010786E2CE8FC901896E83870E00" | ||
``` | ||
|
||
```http | ||
HTTP/1.1 200 OK | ||
Server: Kestrel | ||
ETag: "356075D903B8FE8D9921201A7E7CD3F9" | ||
``` |
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using System.Linq; | ||
using System.Security.Cryptography; | ||
using System.Text; | ||
using Microsoft.Net.Http.Headers; | ||
|
||
namespace JsonApiDotNetCore.Serialization | ||
{ | ||
/// <inheritdoc /> | ||
internal sealed class ETagGenerator : IETagGenerator | ||
{ | ||
private static readonly uint[] LookupTable = Enumerable.Range(0, 256).Select(ToLookupEntry).ToArray(); | ||
|
||
private static uint ToLookupEntry(int index) | ||
{ | ||
string hex = index.ToString("X2"); | ||
return hex[0] + ((uint)hex[1] << 16); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public EntityTagHeaderValue Generate(string requestUrl, string responseBody) | ||
{ | ||
byte[] buffer = Encoding.UTF8.GetBytes(requestUrl + "|" + responseBody); | ||
|
||
using HashAlgorithm hashAlgorithm = MD5.Create(); | ||
byte[] hash = hashAlgorithm.ComputeHash(buffer); | ||
|
||
string eTagValue = "\"" + ByteArrayToHex(hash) + "\""; | ||
return EntityTagHeaderValue.Parse(eTagValue); | ||
} | ||
|
||
// https://stackoverflow.com/questions/311165/how-do-you-convert-a-byte-array-to-a-hexadecimal-string-and-vice-versa | ||
private static string ByteArrayToHex(byte[] bytes) | ||
{ | ||
char[] buffer = new char[bytes.Length * 2]; | ||
|
||
for (int index = 0; index < bytes.Length; index++) | ||
{ | ||
uint value = LookupTable[bytes[index]]; | ||
buffer[2 * index] = (char)value; | ||
buffer[2 * index + 1] = (char)(value >> 16); | ||
} | ||
|
||
return new string(buffer); | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using Microsoft.Net.Http.Headers; | ||
|
||
namespace JsonApiDotNetCore.Serialization | ||
{ | ||
/// <summary> | ||
/// Provides generation of an ETag HTTP response header. | ||
/// </summary> | ||
public interface IETagGenerator | ||
{ | ||
/// <summary> | ||
/// Generates an ETag HTTP response header value for the response to an incoming request. | ||
/// </summary> | ||
/// <param name="requestUrl"> | ||
/// The incoming request URL, including query string. | ||
/// </param> | ||
/// <param name="responseBody"> | ||
/// The produced response body. | ||
/// </param> | ||
/// <returns> | ||
/// The ETag, or <c>null</c> to disable saving bandwidth. | ||
/// </returns> | ||
public EntityTagHeaderValue Generate(string requestUrl, string responseBody); | ||
} | ||
} |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.