| | |
|
|
|
Validators and validation
Caching in HTTP gains improved efficiency through validation mechanisms, including entity tags (Etags) and last-modified dates. This section describes the high-level support for validation available in Zero.
Within Zero, static-file serving supports ETags and caching.
Validators
Etags
Entity tags (etags) are opaque strings associated with a representation (HTTP response body); an etag changes only when the representation changes.
An etag is commonly a hash of the information that affects your resource's representation. For example, for a representation derived from a database query and a rendering template, the associated etag might be a hash of the last-update time from the database and the template's last-modified time.
Zero provides utility APIs for hashing and setting the Etag response header. In Java:
import zero.core.etags.Etag;
...
String concatenatedInformation = ... /* contains the distinguishing information */
String etag = Etag.computeHashedValue(concatenatedInformation);
...
Etag.setEtagHeader(etag);
Last-modified date
Zero automatically adds a Last-Modified response header when serving static files. The value is the last modified time of the served file, formatted according to RFC1123 (e.g. Wed, 22 May 1996 12:06:00 GMT).
For other cases where you want to manually set the Last-Modified header, you can do so with the Global Context and the HttpDateFormat utility formatter. For example, in Java:
zput(Request.Headers.out + "/Last-Modified",
new zero.util.HttpDateFormat().format(new java.util.Date()));
And in Groovy:
headers.out.['Last-Modified'] = new zero.util.HttpDateFormat().format(new Date()))
Validation
"Validation" is the process of checking whether validators (presented by the client) match the current state. Validation can be used as the basis of conditional GET and PUT requests. For example, the response to a "conditional GET", if the representation hasn't changed, could be a bandwidth-saving 304 Not Modified. Also, a "conditional PUT" could be denied if the submitted etag no longer matches -- which prevents the "lost update" problem.
Zero provides the PreconditionValidator utility, which enables you to integrate validation into your handlers:
boolean valid = PreconditionValidator.validate(String etag);
boolean valid = PreconditionValidator.validate(String etag, Date lastModified);
This table represents the possible results of evaluating precondition headers. If multiple headers are present they all must agree that the conditions are not met, otherwise true will be returned to indicate the request should be executed.
| Header Type | Condition Met | Condition Not Met |
| if-match | return true | status code set to 412, return false |
| if-none-match (GET/HEAD) | return true | status code set to 304, return false |
| if-none-match (other HTTP methods) | return true | status code set to 412, return false |
| if-modified-since | return true | status code set to 304, return false |
| if-unmodified-since | return true | status code set to 304, return false |
|
|
r7 - 09 Nov 2007 - 12:05:48 - steveims
|
|
|
| | |