HTTP Range middleware for the Clojure Ring server
Given a response body and a valid Range header, this middleware fulfills the request's Range header. If the content length of the body is known, the bytes are streamed to the client. Otherwise, serverside buffering is required (options can be passed for buffer size).
For java.io.File bodies, ranges are served by seeking with RandomAccessFile (O(range size)), which matters for large media files. Other bodies with a known length are filtered while streaming and stop reading once the requested ranges are satisfied.
The middleware looks for the following to determine the content length:
ring.core.protocols/write-body-to-stream needs to be called to stream the bytes to the client. The servlet and jetty adapter do this automatically. If you aren't using these, you will need to find a way to call this protocol method.Leinengen
[ring-range-middleware "0.2.0"]
You can use it like normal Ring middleware.
(require [ring-range-middleware.core :as range-middleware])
...
(-> your-handler
...
(range-middleware/wrap-range-header)
...)
You can use the handler with options.
(-> your-handler
...
(range-middleware/wrap-range-header opts)
...)
opts is a map with the following keys:
:boundary-generator-fn
:max-num-ranges
:max-buffer-size-per-range-bytes
MIT
Can you improve this documentation?Edit on GitHub
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |