Skip to content

Commit

Permalink
Update the guide with range requests support
Browse files Browse the repository at this point in the history
Also update the list of headers cowboy_rest might set
and tweak a small number of other items.
  • Loading branch information
essen committed Jan 23, 2024
1 parent 4ffcbfb commit 427a276
Show file tree
Hide file tree
Showing 8 changed files with 1,406 additions and 381 deletions.
17 changes: 16 additions & 1 deletion doc/src/guide/resource_design.asciidoc
Expand Up @@ -144,6 +144,16 @@ never be called.
Implement the `languages_provided` or `charsets_provided`
callbacks if applicable.

Does the resource accept ranged requests? If it does,
implement the `ranges_provided` callback. Resources that
only accept `bytes` units can use the callback name
`auto` and let Cowboy automatically do ranged responses.
Other callbacks should have a name prefix of `ranged_`
for clarity. For example, `ranged_bytes` or `ranged_pages`.
If the resource needs to perform additional checks before
accepting to do a ranged responses, implement the
`range_satisfiable` callback.

Is there any other header that may make the representation
of the resource vary? Implement the `variances` callback.

Expand Down Expand Up @@ -191,10 +201,15 @@ the `options` method.
=== GET and HEAD methods

If you implement the methods GET and/or HEAD, you must
implement one `ProvideResource` callback for each
implement one `ProvideCallback` callback for each
content-type returned by the `content_types_provided`
callback.

When range requests are accepted, you must implement one
`RangeCallback` for each range unit returned by
`ranges_provided` (unless `auto` was used). This is
in addition to the `ProvideCallback` callback.

=== PUT, POST and PATCH methods

If you implement the methods PUT, POST and/or PATCH,
Expand Down
Binary file modified doc/src/guide/rest_conneg.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
277 changes: 169 additions & 108 deletions doc/src/guide/rest_conneg.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 15 additions & 3 deletions doc/src/guide/rest_flowcharts.asciidoc
Expand Up @@ -95,6 +95,11 @@ callback will only be called at the end of the
"GET and HEAD methods" diagram, when all conditions
have been met.

Optionally, the `ranges_provided` also returns the
name of a callback for every range unit it accepts. This
will be called at the end of the "GET and HEAD methods"
diagram in the case of ranged requests.

The selected content-type, language and charset are
saved as meta values in the Req object. You *should*
use the appropriate representation if you set a
Expand All @@ -121,11 +126,18 @@ succeed, the resource can be retrieved.

Cowboy prepares the response by first retrieving
metadata about the representation, then by calling
the `ProvideResource` callback. This is the callback
the `ProvideCallback` callback. This is the callback
you defined for each content-types you returned from
`content_types_provided`. This callback returns the body
that will be sent back to the client, or a fun if the
body must be streamed.
that will be sent back to the client.

For ranged requests, but only when the `ranges_provided`
callback was defined earlier, Cowboy will add the selected
`range` information to the Req object and call the
`range_satisfiable` callback. After confirming that the
range can be provided, Cowboy calls the `RangeResource`
callback and produces a ranged response using the
ranged data from the callback.

When the resource does not exist, Cowboy will figure out
whether the resource existed previously, and if so whether
Expand Down
Binary file modified doc/src/guide/rest_get_head.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 427a276

Please sign in to comment.