HTTP caching support is also quite easy, with functions allowing resources to define (e.g.) last_modified
, expires
, and generate_etag
. For instance, since representations of this resource vary only by URI Path, I could use an extremely simple entity tag unfit for most real applications but sufficient for this example:
generate_etag(ReqData, Context) -> {wrq:raw_path(ReqData), ReqData, Context}.
Similarly, here's a trivial expires rule:
expires(ReqData, Context) -> {{{2021,1,1},{0,0,0}}, ReqData, Context}.
And now the response from our earlier request is appropriately tagged:
HTTP/1.1 200 OK
Vary: Accept
Server: MochiWeb/1.1 WebMachine/0.97
Expires: Fri, 01 Jan 2021 00:00:00 GMT
ETag: /demo/authdemo
Date: Sun, 15 Mar 2009 02:59:02 GMT
Content-Type: text/html
Content-Length: 59
<html><body>Hello authdemo from Webmachine.
</body></html>